代码之家  ›  专栏  ›  技术社区  ›  Anupama Boorlagadda

注释以记录Junit5中每个测试用例的执行时间

  •  4
  • Anupama Boorlagadda  · 技术社区  · 8 年前

    我需要记录每个测试用例的执行时间。我不想使用自定义实现。我想知道junit5中是否有对此可用的注释

    Stopwatch 或Junit4 @Timeout .

    5 回复  |  直到 8 年前
        1
  •  6
  •   Anupama Boorlagadda    8 年前

    TimeExtension

    这段代码来自他们的文档:

    @ExtendWith(TimingExtension.class)
    class TimingExtensionTests {
    
      @Test
      void sleep20ms() throws Exception {
         Thread.sleep(20);
      }
    
      @Test
      void sleep50ms() throws Exception {
        Thread.sleep(50);
      }
    
    }
    

    Source code for TimingExtension

        2
  •  2
  •   Nicolai Parlog    8 年前

    我写了一个 BenchmarkExtension 你可以申请 @Benchmark 。您可以按如下方式使用它:

    @Benchmark
    class BenchmarkTest {
    
        @Test
        void notBenchmarked() {
            assertTrue(true);
        }
    
        @Test
        @Benchmark
        void benchmarked() throws InterruptedException {
            Thread.sleep(100);
            assertTrue(true);
        }
    
    }
    

    当应用于该类时,您将获得一条消息,其中包含该类中所有测试方法的总运行时间。当应用于单个测试方法时,您将仅获得该测试的消息。

    JUnit Pioneer .

        3
  •  0
  •   rmuller    5 年前

    Junit5还有一个 @Timout 注释。

    还有一个 assertTimeout assertTimeoutPreemptively (参见 documentation

    最后,JUnit 4秒表功能在JUnit 5中不可用 TimeExtension (还没有?)部分分布(参见 issue 343 ).

        4
  •  0
  •   mourad m    4 年前

    如果您使用的是Sonarqube,您可以找到每个测试的持续时间以及按时间排序的所有测试的总数

    test time sorted

    all test

        5
  •  0
  •   Ironluca    3 年前

    时间就在XML报告中。

    在“testCase”元素上,有一个属性“time”,它给出了执行测试用例的时间。下面是一个例子。时间以秒为单位

    <testcase name="checkZKConnectivityWithAuth" 
              classname="test.zk.ZookeeperConnectionProviderTests" 
              time="7.177"/>
    
    <testcase name="checkConfigRetrieval" 
              classname="test.zk.ZookeeperConnectionProviderTests" 
              time="0.213"/>
    
    <testcase name="checkConfigInsertion" 
              classname="test.zk.ZookeeperConnectionProviderTests" 
              time="0.255"/>