使用nunit控制台传递测试用例参数
我正在使用Nunit和数据驱动的测试方法开发测试。 我有2个参数的测试方法:xlsx文件的路径和工作表名称。 当我在TestCase
属性中传递参数时,它在Visual Studio中工作得很好,例如,当我想运行3个测试用例时,必须编写如下内容:
[TestCase(@"pathToFile.xlsx", "TestCase1")] [TestCase(@"pathToFile.xlsx", "TestCase2")] [TestCase(@"pathToFile.xlsx", "TestCase3")] public void performActionsByWorksheet(string excelFilePath, string worksheetName) { //test code }
我想运行我的测试用例并使用Nunit Console传递参数(不要在代码中编写参数)。
有可能实现吗?
如果您使用的是NUnit 3,则可以使用TestContext.Parameters属性:
[Test] public void performActionsByWorksheet() { string excelFilePath = TestContext.Parameters["excelFilePath"]; string worksheetName = TestContext.Parameters["worksheetName"]; TestContext.WriteLine(excelFilePath); TestContext.WriteLine(worksheetName); }
和–params命令行参数:
nunit3-console.exe path/to/your/test.dll --params=excelFilePath=testPath;worksheetName=testName
我使用TestCaseSource
找到了许多测试用例的解决方法。
测试代码:
[Test, TestCaseSource("testData")] public void performActionsByWorksheet(string excelFilePath, string worksheetName) { Console.WriteLine("excel filePath: {0}", excelFilePath); Console.WriteLine("worksheet Name: {0}", worksheetName); }
从csv文件获取测试数据:
static object[] testData() { var reader = new StreamReader(File.OpenRead(@"TestCases.csv")); List
我在csv文件中存储了我想要运行的所有测试用例(文件路径和工作表名称)。 也许不是最好的解决方案,但我实现了我的目标 – 不在代码中编写参数。