【问题标题】:How to Use Excel DataSource on TestFixture/TestClass C# Selenium Unit Test如何在 TestFixture/TestClass C# Selenium 单元测试中使用 Excel 数据源
【发布时间】:2019-11-15 21:29:43
【问题描述】:

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

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

像这样;

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

    [Test,Order(2)]
    testcodeForNextPage

}

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

在运行时,第一个测试块将为第一行运行在名为MyTable 的数据表上,第二个测试块将针对名为SecondTable 的数据表的第一行运行。由于这些测试是由前一个测试块触发的,所以我无法将数据源提供给测试块。

我在 Internet 上进行了查找,但找不到任何关于将 Datatable 传递到 TestFixture 的信息。在此先感谢人们:)

【问题讨论】:

    标签: c# selenium testing nunit


    【解决方案1】:

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

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

    这是使用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
        }
    
    }
    

    【讨论】:

    • 谢谢查理!我以前见过这个 IEnumerable DataFromExcel 东西,但只用于测试块,不知道它可以用于 TestFixture。我真的很感激你的回答。
    猜你喜欢
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 2016-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多