如何在 TestFixture/TestClass C# Selenium 单元测试中使用 Excel DataSource

How to Use Excel DataSource on TestFixture/TestClass C# Selenium Unit Test

我目前是一家保险公司的 QA 实习生,我正在该公司的网站上做一些测试。我已经完成了很多案例,现在他们要求进行数据驱动测试,而我正在为此苦苦挣扎。

我已经完成了所有测试;
1 个测试夹具
测试测试用例中的每个页面。

像这样;

[TestFixture]
public class Test : BaseClassForTheTest
{
    [Test, Order(1)]
    TestcodeForHomePage

    [Test,Order(2)]
    testcodeForNextPage

}

所以我需要 运行 完整测试,用于 excel 文件中的许多数据。正如您可能已经注意到的那样,我正在使用 NUnit。
真正的问题是,如何将 DataTable 传递到 TestFixture 并为数据表制作测试块 运行。

On 运行,第一个测试块将 运行 用于名为 MyTable 的数据表的第一行,第二个测试块将 运行 用于名为 [=14] 的数据表的第一行=].由于这些测试是由前一个测试块触发的,我无法将数据源提供给测试块。

我在 Internet 上查找过,但找不到有关将 Datatable 传递到 TestFixture 的任何信息。在此先感谢大家:)

NUnit 中没有内置任何内容来读取 Excel 文件。但是您可以使用 TestCaseSourceTestFixtureSource 从您喜欢的任何地方生成数据。

您的来源必须是一个方法,然后它将读取 excel 文件和 return 正确的参数。

这是使用 TestCaseSource...

的大纲
[TestFixtureSource("DataFromExcel")]
public class MyTestFixture : BaseClassForTheTest
{
    IEnumerable<TestCaseData> DataFromExcel()
    {
        // Read the Excel file
        // For each row of data you want to use
        //     yield return new TestCaseData(/*test fixture args here*/);
    }

    public MyTestFixture(/* your arg list */)
    {
        // Save each arg in a private member
    }

    [Test, Order(1)]
    TestcodeForHomePage()
    {
        // Code that uses the saved values from the constructor
    }

    [Test,Order(2)]
    TestcodeForNextPage()
    {
        // Code that uses the saved values from the constructor
    }

}