2016-07-13 16 views
1

我正在使用Nunit3版本中提供的參數參數傳遞多個參數。在C#測試夾具中從Nunit3獲取參數

但是,我無法使用C#測試夾具來獲取它們。我已經搜索,但無法得到正確的結果。

有人可以提供關於如何在c#中獲取這些參數參數的指針。

任何幫助將不勝感激。提前致謝。

+0

燦你發佈你的代碼吧? –

+0

nunit3-console「D:\ Codebase \ Automation \ ReportGenerationAutomation \ bin \ Debug \ Automation.dll」--test = Automation.Report --params:Code = XXX --params:Date = 2011-05-16 --work =「D:\ Codebase \ Automation \ ReportGenerationAutomation \ bin \ Debug」 – Niraj

+0

這是nunit3的命令。我需要在C#中獲取參數Date和Code。此命令運行成功,但我被困在檢索部分,因此,該部分沒有C#代碼可用。 – Niraj

回答

0

首先,確保您同時使用NUnit控制檯3.4.1和NUnit Framework 3.4.1。

您的命令行選項--params:Code=XXX --params:Date=2011-05-16看起來正確。您還可以將多個參數與分號組合使用,--params:Code=XXX;Date=2011-05-16

要訪問單元測試中的參數,請在測試中使用TestContext.Parameters.Get("Code")。還有一個string Get(string key, string default)和一個T Get(string key, T default)它做一個Convert.ChangeType

目前還沒有很好的記錄,所以請參閱pull request that implemented the feature瞭解更多信息。

下面是一個示例性測試,

[Test] 
public void TestCommandLineParameters() 
{ 
    var code = TestContext.Parameters.Get("Code", "<unknown>"); 
    var date = TestContext.Parameters.Get("Date", DateTime.MinValue); 

    TestContext.WriteLine($"Fetched test parameters {code} and {date}"); 
} 

我與命令行和NUnit 3.4.1運行,

nunit3-console.exe --params:Code=XXX --params:Date=2011-05-16 .\nunit-v3.dll 

在輸出中,我看到

=> nunit.v3.TestParamsTest.TestCommandLineParameters 
Fetched test parameters XXX and 2011-05-16 12:00:00 AM 
+0

嘿羅布......很好的答案,但我們對命令行部分和TestContext部分都有文檔。如果我們需要其他的東西,比如一篇how-to文章,也許我們可以讓一些用戶爲我們編寫它。我沒有一個實際的用例可能導致很好的寫作。 – Charlie