简体   繁体   English

TestNG和JUnit执行之间的区别

[英]Difference between TestNG and JUnit execution

I was going through the differences between JUnit and TestNG and the advantages that TestNG has over Junit. 我正在研究JUnit和TestNG之间的区别以及TestNG与Junit相比的优势。 I read that JUnit creates an instance of the class for each and every test method that it runs in that class. 我读到JUnit为在该类中运行的每个测试方法创建该类的实例。 The advantage here is an error in one test method does not propagate to other test method and fail the other test method. 这样做的好处是,一种测试方法中的错误不会传播到另一种测试方法,而会使另一种测试方法失败。 TestNG on the other hand supports testing integrated classes which means by default, no need to create a new test class instance for every test method. 另一方面,TestNG支持测试集成类,这意味着默认情况下,无需为每个测试方法创建新的测试类实例。

Can somebody kindly help me on how this works? 有人可以帮我解决这个问题吗? I am not able to understand the propagation of failure. 我无法理解故障的传播。 Does anyone has an example that clearly explains this difference? 有没有人能清楚地说明这种差异的示例?

Regards Srinivas 问候斯里尼瓦斯

I used JUnit and TestNg for long time. 我长时间使用了JUnit和TestNg。 I am not sure what yo uare referring to in question. 我不确定您所指的是什么。 So I mention feature which makes TestNg my favorite. 因此,我提到了使TestNg成为我的最爱的功能。 Otherwise they are comparable. 否则它们是可比的。

Parametrized tests: 参数化测试:

JUnit is not very handy for parametrized tests, because when you are using using @Parametrized , test parameters are passed from static method into constructor of test class. JUnit对于参数化测试不是很方便,因为在使用@Parametrized ,测试参数从静态方法传递到测试类的构造函数中。 So obviously you can have only one set of parameters per class. 因此,显然每个类只能有一组参数。 SO you are forced to create separate testing class per test case when using parameters. 因此,在使用参数时,您不得不为每个测试用例创建单独的测试类。 Here is example of such test . 这是这种测试的例子

On the other hand, TestNg has concept of @DataProvider , which enables you to have various parameter groups in one test class. 在另一方面,TestNG的有概念@DataProvider ,使您能够有不同的参数组中的一个测试类。 So you can combine various tests with various parameters in one test class. 因此,您可以在一个测试类中将各种测试与各种参数结合在一起。 Example here . 这里的例子

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

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