You need to enable JavaScript to run this app. C# NUnit3框架下ExtentReport仅显示最后一个测试类结果问题咨询
优惠活动
大模型
产品
解决方案
定价
更多

C# NUnit3框架下ExtentReport仅显示最后一个测试类结果问题咨询

问题根因
  • 你错误地将标注了[SetUpFixture]的全局配置类作为所有测试类的基类使用:NUnit中[SetUpFixture]的作用是定义整个命名空间范围内的一次性初始化/销毁逻辑,但当测试类继承该类后,基类的[OneTimeSetUp]和[OneTimeTearDown]会成为每个派生测试类的生命周期方法,每执行完一个测试类就会触发一次:
    1. 每次执行新测试类的OneTimeSetUp时都会重新实例化ExtentReports对象,之前存储的所有测试结果都会被清空
    2. 每次测试类执行完都会调用extent.Flush()将当前仅有的单个测试类结果写入报告,后续测试类的写入操作会直接覆盖之前的报告文件
  • 静态变量extentTest会在多个测试用例/测试类执行时被互相覆盖,容易导致日志归属错误
解决方案

将全局Extent配置逻辑和测试基类拆分,二者职责分开:

  1. 保留带[SetUpFixture]的类作为全局配置类,不要让任何测试类继承它,它仅负责整个测试执行周期的一次Extent初始化和最终一次Flush操作
  2. 单独定义无[SetUpFixture]的测试基类,所有[TestFixture]测试类继承该基类,处理每个测试用例的驱动初始化、测试节点创建等逻辑

修正后代码示例

1. 全局Extent配置类(独立存在,不被继承)

using NUnit.Framework;
using AventStack.ExtentReports;
using AventStack.ExtentReports.Reporter;

[SetUpFixture]
public class ExtentReportConfig
{
    public static ExtentReports extent;
    // 替换为你实际的报告生成路径
    private readonly string reportPath = @"C:\TestReports\TestReport.html";

    [OneTimeSetUp]
    public void GlobalSetup()
    {
        var htmlReporter = new ExtentHtmlReporter(reportPath);
        extent = new ExtentReports();
        extent.AttachReporter(htmlReporter);

        htmlReporter.Config.DocumentTitle = "Tests Report";
        htmlReporter.Config.ReportName = "Issta Tests Report";

        extent.AddSystemInfo("OS", "Windows 10");
        extent.AddSystemInfo("Environment", "QA");
        extent.AddSystemInfo("User Name", "Valeri Rozenberg");
    }

    [OneTimeTearDown]
    public void GlobalTearDown()
    {
        // 整个测试所有用例执行完才会触发一次Flush,不会被覆盖
        extent.Flush();
        // Email.sendEmail(reportPath);
    }
}

2. 测试基类(所有测试类继承该类)

using NUnit.Framework;
using AventStack.ExtentReports;
using OpenQA.Selenium;

public class BaseTest
{
    protected IWebDriver driver;
    protected ExtentTest extentTest;
    // 替换为你实际的测试地址
    protected readonly string baseUrl = "https://your-test-site.com";

    [SetUp]
    public void TestSetup()
    {
        // 你的驱动初始化逻辑
        driver = DriverManagerFactory.GetDriverManager(DriverType.Chrome).GetWebDriver();
        driver.Url = baseUrl;
        // 每个测试用例执行前创建对应的测试节点,用当前测试用例名作为节点名
        extentTest = ExtentReportConfig.extent.CreateTest(TestContext.CurrentContext.Test.Name);
    }

    [TearDown]
    public void TestTearDown()
    {
        // 自动根据测试用例执行结果打日志,不用每个用例手动写Pass日志
        var testResult = TestContext.CurrentContext.Result.Outcome.Status;
        if (testResult == NUnit.Framework.Interfaces.TestStatus.Failed)
        {
            extentTest.Log(Status.Fail, $"测试失败:{TestContext.CurrentContext.Result.Message}");
            // 可选:失败时截图添加到报告
        }
        else if (testResult == NUnit.Framework.Interfaces.TestStatus.Passed)
        {
            extentTest.Log(Status.Pass, "测试执行通过");
        }
        driver.Quit();
        driver.Dispose();
    }
}

3. 修改后的测试类

namespace AutomationTests
{
    [TestFixture]
    public class AutomaticDeals : BaseTest
    {
        [Test]
        public void TestLinks()
        {
            AutomaticDealsPage aDeals = new AutomaticDealsPage(driver);
            Assert.IsTrue(aDeals.CheckEqualUrls(1));
            // 自定义日志可以直接在这加:extentTest.Log(Status.Info, "自定义信息");
        }

        [Test]
        public void TestPrices()
        {
            AutomaticDealsPage aDeals = new AutomaticDealsPage(driver);
            Assert.IsTrue(aDeals.allPricesEqual());
        }
    }
}
注意事项
  • 确保ExtentReportConfig类和所有测试类处于同一个命名空间下,如果你需要适配多个命名空间的测试,可以将ExtentReportConfig放在没有命名空间的全局位置
  • 如果你的测试是多线程执行的,需要给extent.CreateTest操作加锁,避免多线程写入冲突

内容的提问来源于stack exchange,提问作者ValeriF21

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.10 08:36:01