pass test case parameters using nunit console

一曲冷凌霜 提交于 2019-12-02 23:09:44

If you are using NUnit 3 you can use TestContext.Parameters property:

[Test]
public void performActionsByWorksheet()
{
    string excelFilePath = TestContext.Parameters["excelFilePath"];
    string worksheetName = TestContext.Parameters["worksheetName"];
    TestContext.WriteLine(excelFilePath);
    TestContext.WriteLine(worksheetName);
}

and --params command line argument:

nunit3-console.exe path/to/your/test.dll --params=excelFilePath=testPath;worksheetName=testName

I found a workaround for many test cases, using TestCaseSource.
Test code:

[Test, TestCaseSource("testData")]
public void performActionsByWorksheet(string excelFilePath, string worksheetName)
{
    Console.WriteLine("excel filePath: {0}", excelFilePath);
    Console.WriteLine("worksheet Name: {0}", worksheetName);
}

Getting test data from csv file:

static object[] testData()
{
    var reader = new StreamReader(File.OpenRead(@"TestCases.csv"));
    List<object[]> rows = new List<object[]>();

    while (!reader.EndOfStream)
    {
        var line = reader.ReadLine();
        var values = line.Split(',');
        rows.Add(values);
    }

    return rows.ToArray<object[]>();                        
}

and I store all test cases I want to run (file paths and worksheet names) in csv file. Maybe not the best solution, but I achieved my goal - not to write parameters in code.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!