首页 > 解决方案 > 引诱报告中TestCaseSource的分组

问题描述

我目前是使用 NUnit 和 Allure 进行报告的 Selenium 自动化测试的新手。

我已经设法完成了一些简单的 Web 自动化测试场景,并且能够很好地在 Allure 上显示结果。

转到更高级的场景,我正在研究基于多个数据集(在 CSV/Excel 中)递归地执行 [测试]。我读过这可以通过 [TestCaseSource] 实现。

我的第一次尝试是通过命令行 dotnet test --logger:trx 运行 .csproject 来生成报告。对于此尝试,所有 [TestCaseSource] 都被触发,但只有第一个被标记到 TestClass。

easyFormDemo.cs中的[测试]:

[Test, TestCaseSource(typeof(ModelTestCaseSource), "GetTestCaseDaysCSV", new object[] { "../netcoreapp3.1/data.csv" })]
public void formDemo3(DataDays dataDays)
{
    driver.Url = "https://www.seleniumeasy.com/test/basic-first-form-demo.html";

    string sendkey = dataDays.Day;
    IWebElement msg = driver.FindElement(By.Id("user-message"));
    msg.SendKeys(sendkey);

    IWebElement btn = driver.FindElement(By.XPath("//button[text()='Show Message']"));
            btn.Click();

    string display = driver.FindElement(By.Id("display")).Text;

    Assert.AreEqual(sendkey, display);
}

ModelTestCaseSource.cs:

public static IEnumerable<TestCaseData> GetTestCaseDaysCSV(string path)
{
    DataTable dt = CSVReader.ConvertCSVtoDataTable(path);
    
    foreach (DataRow row in dt.Rows)
    {
        var dataDays = new DataDays();
        dataDays.Day = row["Day"].ToString();
        yield return new TestCaseData(new object[] { dataDays });
    }
}

(无法直接上传图片抱歉)

Allure Report Image 通过命令行运行测试: Allure Report Image via command line

从图中可以看出,只有 formDemo3 的第一个 TestCaseSource 附加到了 TestSelenium.easyFormDemo。formDemo3 的 TestCaseSource 的其余部分分别附加到底部。

然后我环顾四周,阅读了有关 NUnit.Allure nuget 包的信息。我导入了 nuget 并相应地配置了 [AllureParentSuite] 和 [AllureSuite]。

easyFormDemo.cs中的[测试]:

[AllureParentSuite("FormDemo")]
.
.
.
[Test, TestCaseSource(typeof(ModelTestCaseSource), "GetTestCaseDaysCSV", new object[] { "../netcoreapp3.1/data.csv" })]
[AllureSuite("FormDemo3")]
public void formDemo3(DataDays dataDays)
{
    driver.Url = "https://www.seleniumeasy.com/test/basic-first-form-demo.html";

    string sendkey = dataDays.Day;
    IWebElement msg = driver.FindElement(By.Id("user-message"));
    msg.SendKeys(sendkey);

    IWebElement btn = driver.FindElement(By.XPath("//button[text()='Show Message']"));
            btn.Click();

    string display = driver.FindElement(By.Id("display")).Text;

    Assert.AreEqual(sendkey, display);
}

然后我通过 Visual Studio 测试查看器触发了该项目。现在的问题是,虽然所有的 TestCaseSource 都被触发了,但只有第一个 TestCaseSource 被捕获到 Allure Report 中。

使用 AllureSuite 运行的 Allure 报告图像:通过 AllureSuite的 Allure 报告图像

任何帮助将不胜感激,因为我目前无法找到任何链接/帮助。

标签: c#seleniumnunitallure

解决方案


理想情况下,您应该从您的 TestCaseSource 生成 IEnumerable。

使用 TestCaseSource 时,生成唯一的测试名称以使报告工作很重要。更重要的是,一个好的报表名称会让你的报表看起来很棒。

TestCaseSource 用法

public static IEnumerable<TestCaseData> TestCaseSource() =>         
    MyTestCaseSourceFunction();

[TestCaseSource(nameof(TestCaseSource))]
public void CanDiscoverEndPoints(string param1)

构造一个 TestCaseData

Return an IEnumerable<TestCaseData> for your purpose.

new TestCaseData("parameter value")
{
    TestName = $"MyTestName-{apiRoot}{api}{op.Operation.Method}"
}

推荐阅读