【问题标题】:How to assign TestNG test results to use cases?如何将 TestNG 测试结果分配给用例?
【发布时间】:2025-12-07 16:20:06
【问题描述】:

我需要将我的测试结果分配给用例。 目前,我的课程有 TestNG 测试(单元测试)。 显然这些测试是因为用例而存在的类,但没有明显的1-1映射。

是否可以配置 TestNG 报告以在报告中包含自定义组?

喜欢

F02UC01 解析输入 对于这个用例,我有测试类:

  • com.company.product.input.ParseTest

F03UC02产生输出 对于这个用例,我已经测试了

  • com.company.product.input.OutputTest com.company.product.input.AnotherOutputTest

理想情况下,我不想重新运行或重写现有测试。我只想要一份不同分组标准的测试报告。

【问题讨论】:

    标签: unit-testing testing testng test-reporting


    【解决方案1】:

    通常您通过创建套件 xml 文件来执行此操作。例如

    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
    <suite name="Use Case Tests">
    
      <test name="F02UC01 parse input">
        <classes>
          <class name="com.company.product.input.ParseTest"/>
        </classes>
      </test>
    
      <test name="F03UC02 produce output">
        <classes>
          <class name="com.company.product.input.OutputTest"/>
          <class name="com.company.product.input.AnotherOutputTest"/>
        </classes>
      </test>
    
    </suite>
    

    也许您可以通过包而不是类来选择它们。

    <packages>
      <package name="com.company.product.input.*"/>
    </packages>
    

    您还可以混合使用classespackages 选择。请参阅testng documentation

    编辑

    无论如何,我正在运行所有测试。我只想要一份报告,其中一些测试是用例。

    我猜在这种情况下你必须实现自己的IReporter

    我将创建一个注释,我可以将其添加到测试方法中以对它们进行逻辑分组。例如

    @Test
    @TestTag("F02UC01 parse input")
    public void someTest(){
    }
    

    然后在我的自定义报告器中使用IAnnotationFinder 来报告按注释值分组的测试。

    【讨论】:

    • 看起来不错!在这种情况下,是否有可能仅仅为了这份报告而避免重新运行测试?无论如何,我正在运行所有测试。我只想要一份报告,其中一些测试是用例。
    最近更新 更多