原文链接:http://wudashan.com/2020/09/13/testng-learning/
框架介绍
英文原版
TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionalities that make it more powerful and easier to use, such as:
* Annotations.
* Run your tests in arbitrarily big thread pools with various policies available (all methods in their own thread, one thread per test class, etc...).
* Test that your code is multithread safe.
* Flexible test configuration.
* Support for data-driven testing (with @DataProvider).
* Support for parameters.
* Powerful execution model (no more TestSuite).
* Supported by a variety of tools and plug-ins (Eclipse, IDEA, Maven, etc...).
* Embeds BeanShell for further flexibility.
* Default JDK functions for runtime and logging (no dependencies).
* Dependent methods for application server testing.
TestNG is designed to cover all categories of tests: unit, functional, end-to-end, integration, etc...
中文翻译
TestNG是一个受JUnit和NUnit启发的测试框架,但引入了一些使其更强大且更易于使用的新功能,例如:
- 注解。
- 线程池中运行测试用例。
- 支持测试代码是否多线程安全。
- 灵活的测试配置。
- 支持数据驱动的测试(使用@DataProvider)。
- 以插件形式被各种工具(Eclipse,IDEA,Maven等)集成。
TestNG旨在涵盖所有类别的测试:单元,功能,端到端,集成等。
源码版本
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8</version>
<scope>test</scope>
</dependency>
带着问题去学习
通过TestNG框架的官方介绍,我们知道了它主要提供了哪些功能,对应的我们需要通过几个问题去理解其如何实现(原理)?
注解功能
TestNG如何发现需要被测试的方法?
通过Java两大特性,注解+反射,找到被测方法。具体原理为先通过main函数入参或testng.xml配置文件获取需要扫描的类,再通过反射获取类信息,判断是否有@Test注解,如果有则表示该类的方法需要测试。
TestNG如何支持用户感知框架运行时状态?
通过开放各种Listener接口(父类为org.testng.ITestNGListener)ÿ