简体   繁体   English

在诸如JUnit和TestNG的测试框架中,测试执行侦听器的典型用例是什么?

[英]What are typical uses cases for Test Execution Listeners in Testing Frameworks such as JUnit and TestNG?

JUnit,TestNG, and Spring testing have test execution listeners as an extension mechanism. JUnit,TestNG和Spring测试将测试执行侦听器作为扩展机制。 Test execution listeners seem to be a low level feature of interest to framework developers and tool developers. 测试执行侦听器似乎是框架开发人员和工具开发人员感兴趣的低级功能。

What are the main use cases in which Test execution listeners are useful? 测试执行侦听器有用的主要用例是什么? Can they be useful to application developers? 它们对应用程序开发人员有用吗?

In Case of testng the listners are the most remarkable thing for me. 对于testng而言,对于我来说,列出者是最杰出的事情。 The testNg listners allows me to simplyfy the test method content and manage each test with server startup , user registration, populating artifacts need for tests . testNg列表器使我可以简单地确定测试方法的内容,并通过服务器启动,用户注册以及填充测试所需的工件来管理每个测试。 And also clear up the environment for each test cycle. 并为每个测试周期清理环境。

At the each listener level im performing following operations so i don't have to bother about them in side my tests 在每个侦听器级别,我都会执行以下操作,因此我不必在测试过程中为它们打扰

IExecutionListener IExecutionListener

   - onExecutionStart()
         Emma instrumentations
         Server start 
  - onExecutionFinish()
         Server Sutdown
         Emma report generation
  • ISuiteListener Set environment properties ex- Key Store Paths ISuiteListener设置环境属性(密钥存储路径除外)

      Populate Users. 
  • ITestListener ITestListener

    • On Start 开始时

        Artifact Deployment 
    • On Finish 在完成

        Artifact Clean up 
  • IReporter iReporter

  • Generate TestNg Report, Generate sure-fire report, Export data for Dashboard 生成TestNg报告,生成保证报告,为仪表板导出数据

For JUnit, the listeners are designed to be a method of reporting, and are used like this internally. 对于JUnit,侦听器被设计为一种报告方法,并且在内部像这样使用。 So, you can display counts of tests executed, success, failure, error, that sort of thing. 因此,您可以显示执行的测试计数,成功,失败,错误等等。 This is used externally as well, such as in maven-surefire . 这也可以在外部使用,例如在maven-surefire中 See JUnit4RunListener.java as an example. 请参见JUnit4RunListener.java作为示例。

Another use would be to output in a different format, such as XML. 另一个用途是以其他格式输出,例如XML。 I think the main use cases for the other frameworks are the same. 我认为其他框架的主要用例是相同的。

In JUnit, the listener class is not meant to be used in the manner that Dharshana uses his test listeners in testng, that is as a setup/teardown. 在JUnit中,不应该以Dharshana在testng中使用其测试监听器的方式使用监听器类,即,将其用作设置/拆卸。 The objects used in the Listeners ( Description , Failure , Result ) are immutable and don't encourage direct access to the test class itself. 侦听器中使用的对象( DescriptionFailureResult )是不可变的,并且不鼓励直接访问测试类本身。 I'm not sure about TestNG, Cedric would be a better person to ask about that. 我不确定TestNG,Cedric会是一个更好的人。

Are they useful for an application developers? 它们对应用程序开发人员有用吗? They may well be, depending upon how your tests are set up. 可能取决于测试的设置方式。 They would only be used in the context of tests, so if they can improve them, then go ahead, use them. 它们只会在测试的环境中使用,因此,如果它们可以改进它们,那么请继续使用它们。 One use case would be JUnit test report enrichment with JavaDoc , see my answer . 一种用例是使用JavaDoc丰富JUnit测试报告 ,请参阅我的回答 To recap the answer, if the developers add a test for a specific bug, then they can add an annotation to that test linking it back to the bug. 概括一下答案,如果开发人员为特定错误添加了测试,则他们可以在该测试中添加注释,将其链接回该错误。 There is a custom RunListener, which collects all of the information in the annotations together and produces a report for the final customer. 有一个自定义的RunListener,它将收集注释中的所有信息,并为最终客户生成报告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM