Software development goes through many stages , Such as requirements collection and analysis 、 Design 、 software development 、 Test and release . The test is SDLC An integral part of , Unit testing is a reliable type of testing . image JUnit and TestNG Such an excellent unit testing framework has become the mainstream choice , But what about TestNG And JUnit There has always been a debate about the difference between .
unit testing
Testing is not a single activity , It covers a variety of test scenarios . It is classified in different ways , One is based on the test level , For example, integration 、 Unit and system testing .
Unit testing involves testing the smallest piece of code in a software product . The goal is to check that the quality of each component of the code is performing as expected . It executes during the development phase . Isolate a piece of code to ensure its validity and accuracy . A single component of the code can be a function 、 modular 、 Object or method . Unit testing is always done before integration testing . It helps identify defects early in the application development lifecycle . Developers use different unit test frameworks to create automated test cases for unit tests . There are different tools available on the market to perform unit tests , Such as JUnit、NUnit、PHPUnit、JMockit etc. .
JUnit On 1997 As an open source based Java The unit test framework of . It is XUnit Part of , It is the representative of unit test framework family . It allows developers to write and run repeatable tests .
TestNG It's based on Java The unit test framework of , New and improved features . These new features include flexible test configurations 、 Parameter support 、 Data driven testing 、 notes 、 Various integrations, etc .TestNG execution unit 、 End to end and integration testing .TestNG Generate a report , Help developers understand the passing of all test cases 、 Fail and skip status .
understand TestNG and JUnit Differences between test frameworks , It helps to select the most suitable unit test framework .
TestNG and JUnit The difference between
although TestNG and JUnit Are top-level based on Java Automation framework for , And each has its own advantages and disadvantages . Share below JUnit and TestNG The main differences between frameworks :
test suite
The test suite consists of a set of test cases , Allow tests to be executed at the same time . The test suite functions in JUnit Is not allowed in earlier versions of , But in JUnit 5 Introduction in , and TestNG Early support for this feature . Although both have test suites , But there are key differences in the way they perform tests on each test suite . Let's take a look at the code snippet that shows how the test suite runs in both frameworks .
TestNG The test suite in is from
XML
File run :
<suite name=”TestSuite”>
<test name=”Demo”>
<classes>
<class name=”com.fsecure.demo.testng.TestNGTest1″ />
<class name=”com.fsecure.demo.testng.TestNGTest2″ />
</classes>
</test>
</suite>
And in the JUnit in , Use
@RunWith
and
@Suite
Wait for comment , This is shown in the following code snippet . Two classes JUnit1 and JUnit2 Is to use annotations
@Suite
Compiling .
@RunWith(Suite.class)
@Suite.SuiteClasses({
JUnit1.class,
JUnit2.class
})
public class JunitTest5 {
//code
}
Use TestNG Easier for testers , Because it gives them a variety of options to use the test suite . for example , Test suites can be executed by bundling classes into groups .
notes
PS: stay JUnit 4 in ,
@BeforeClass
and
@AfterClass
Methods are considered static , And in the
TestNG
There is no such limitation in .
Use case management
Managing test execution is an important task ; And JUnit comparison ,TestNG Make the task easier .
Group test
At present, this function only has TestNG Support . It involves performing tasks by creating multiple groups . Each contains various test classes , And you can run tests in separate groups , Instead of running isolated tests . It USES
@Test
Parameters in comments .
@Test(groups={"groupname1",<"group2">..,<"groupN">})
stay TestNG in , Can be in
<test>
or
<suite>
Easy identification under the mark .
Ignore tests
There is no need to perform certain tests from large test suites , Especially if you just want to test certain functions . This feature includes whether specific unit tests should be ignored or considered .JUnit and TestNG Are equipped with this feature , And all the comments discussed earlier . stay JUnit in , This function uses
@ignore
annotation :
@Ignore
public void method1() {
//code
}
And in the TestNG in , It USES
@Test(enabled = false)
Comment run .
@Test(enabled=false)
public void TestWithException(){
//code
}
A parameterized
Parameterization can be understood as data-driven testing , You can reduce the amount of code and improve its readability .TestNG And JUnit This functionality is provided in different ways .TestNG There is a simple way to fix parameters in test cases . It USES
@Parameter
Comment and add parameters to a given test method .
browser
The value of the XML file ( for example
testng.xml
) Description in , and JUnit Use
@ParameterizedTest
notes .
Dependency testing
This function indicates when one test method depends on another test method .JUnit This feature is not currently supported .TestNG Support multiple types of tests . stay TestNG in , Dependent methods use
@DependsOnMethods
annotation .
@Test(dependsOnMethods = {"Login"})
//code
Abnormal test
This function verifies the exception to be used when an error is encountered during test execution .TestNG and JUnit All support this function , However, exceptions are handled in a slightly different way .TestNG stay
@Test
Used in the annotations
expectedException
Parameters .
stay JUnit in ,
assertThrows API
For exception handling :
@Test(expectedExceptions = ArithmeticException.class)
public void FunTester() {
int i = 10/0;
}
Timeout tests
This function refers to the timeout function during test execution , This function sets the time limit , When this time limit is exceeded , The test will fail automatically .TestNG and JUnit All provide this function in the same syntax .
JUnit:
@Test(timeout = 1000)
public void method1() {
// do nothing
}
TestNG:
@Test(timeOut = 1000)
public void testThisShouldFail() {
// do nothing
}
Conclusion
Many test frameworks support automated testing , It depends on the goal of the test .TestNG and JUnit Are the most trusted frameworks in the field of automated unit testing .TestNG Overcome JUnit Many inconveniences , It simplifies the work of testers . Use TestNG, You can perform unit tests 、 Integration testing and end-to-end testing , and JUnit Only unit tests are covered .
Fun·BUG excavating machinery · Performance conqueror · Head pot cover ·Tester
Single measurement & White box
FunTester Community style
Special topic of interface function test
Case sharing : programme 、BUG、 Reptiles
Read the original , Jump to my warehouse address
原网站版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206212132501682.html