【问题标题】:how to get Tests run count on running same testCase for multiple times with different data如何使用不同的数据多次运行相同的 testCase 来获得测试运行计数
【发布时间】:2011-01-18 07:29:18
【问题描述】:

嗨,我想用不同的数据多次运行同一个测试用例

例如,如果我用不同的数据运行相同的测试用例 3 次,它应该会显示

测试运行:3,失败:0

当我尝试让测试运行时:1 失败:0 只有任何建议?

导入 org.junit.Test; 导入 org.junit.experimental.theories.*; 导入 org.junit.runner.RunWith; @RunWith(Theories.class) 公共类 PrimeTest {

@Theory
@Test
public void isPrime(int candidate ){
    System.out.println("candidate: "+ candidate );
}
public static @DataPoints int[] candidates = {1,2,3,4};

}

这里的测试用例 isPrime 运行 4 次意味着检查 4 个测试场景,但最终结果是测试运行:1/1 失败:0 我需要的是应该显示为 测试运行:4/4 失败:0

【问题讨论】:

    标签: java junit4 testcase


    【解决方案1】:

    您应该将不同的数据作为参数传递给测试并使用数据提供者来创建数据。我一点点测试代码将有助于提出更多建议...

    【讨论】:

    • 嗨,下面是示例代码,测试用例运行了 4 次,但在报告中显示为 runtests:1/1 失败:0 我需要的是 runtests:4/4 失败:0以便您可以轻松找到失败案例... import org.junit.Test;导入 org.junit.experimental.theories.*;导入 org.junit.runner.RunWith; /** * @author * */ @RunWith(Theories.class) public class PrimeTest { @Theory @Test public void isPrime(int Candidate){ System.out.println("candidate: "+ Candidate); } 公共静态 @DataPoints int[] 候选人 = {1,2,3,4}; }
    【解决方案2】:

    也许你可以使用parameterized 测试

    【讨论】:

    • 我能够多次运行测试用例,但我只能将测试运行为 1 我应该得到完整计数
    • 但这有什么关系呢?您能否澄清一下像运行测试数这样的语句是 4(比如说)而不是 1 会有帮助吗?您是否想暗示更多数量的测试意味着更好的测试,我认为这是不正确的。语句、分支和条件覆盖等内容可以更好地了解您的单元测试对代码库的覆盖程度。
    猜你喜欢
    • 2014-02-19
    • 2013-06-13
    • 2010-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-02
    • 2016-07-20
    • 1970-01-01
    相关资源
    最近更新 更多