当前位置:网站首页>How to use xUnit framework to maintain test cases?
How to use xUnit framework to maintain test cases?
2022-06-30 19:22:00 【Hua Weiyun】
This is an excellent student of Hogwarts testing college Junit Learning notes . Advanced test development skills , Add group at the end of the text .
1、xUnit What is it?
First look at Wikipedia Explanation above :

xUnit It is the general name of a series of test frameworks , It started with a called Smalltalk Of SUnit frame , Now various object-oriented languages , Such as Java、Python Our ancestors were Smalltalk, Later, these languages used Sunit The idea of framework , There are many general specifications and features , It is also collectively referred to as xUnit.
1.1 xUnit Framework system
Java : JUnit、TestNG
Python : UnitTest、PyTest
1.2 xUnit Common characteristics of
Test Runner : Test runner
Test Case : The test case
Test Fixtures : Test fixture / Fixture , Used to manage the execution of test cases
Test Suites : test suite , Used to orchestrate test cases
Test Execution: The test execution , In what order
Test Result Formatter: test result , Have the same format , Can be integrated
Assertions: Assertion
2、 from Junit4 Turn on xUnit The journey to the framework
2.1 Why from Junit4 Start
Junit4 Still 99% The preferred framework for R & D engineers , Facilitate the communication between test engineers and R & D engineers ( Pull the strings ~~);
TestNG It is mostly used by test engineers ;
Junit5 It has not been popularized on a large scale ( The most recommended framework , mature 、 To use 、 R & D test general purpose );
Many frameworks are based on Junit4 customized ;
2.2 The core elements of a test case
The name of the test case : Property method name
Test case description and label : annotation
Container for test cases : Class or suite
Testing process
unit testing
Web automated testing Selenium
App automated testing Appium
Interface automation testing RestAssured
Test assertion
2.3 basic demo function
1) establish maven engineering XUnit,pom.xml Add Junit rely on ;
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope></dependency>2)src/test/java Create a test class Junit4DemoTest
Be careful
The test class should be Test Beginning or end
maven auto-import
src/main/java Store application implementation code
src/test/java Storage unit test
One of the principles of unit testing : Use cases can run independently
Basic test demo function :

Running results :

2.4 The order of execution between use cases
Junit4:
Default Depending on the list obtained by the reflection method , The order is fixed ( No insurance )
@FixMethodOrder(MethodSorters.JVM) The order may change
@FixMethodOrder(MethodSorters.NAME_ASCENDING) By name ASCII The order ( Stable common , It is recommended to use )
TestNG、Junit5:
You can set the order through annotations Order
Sequential presentation

Running results :

2.5 The execution sequence of the test suite supports

Junit4:
@BeforeClass、@AfterClass
@Before、@After
TestNG:
@BeforeClass
@BeforeMethod
BeforeGroup、@BeforeSuite
Junit5:
@BeforeClass
@BeforeEach
Demonstration of practical operation 1
Add... Before and after use case execution @Before and @After:

Running results :

Demonstration of practical operation 2
add @BeforeClass and @AfterClass

Running results :

2.5 Examples of practical application of use case management ——App Automated test case management
The base class @BeforeClass:
Configure read 、 To configure Capability、 initialization driver、 install App,PageObject initialization
Integrated subclass execution process
@Before: Start and enter specific interface
@Test: Test case execution
@After: Back to the entrance
@BeforeClass: Enter a specific tab Sub function page
@AfterClass: close app
The base class @AfterClass
driver.quit
2.6 Test flow under inheritance relationship
Process sequence :
Parent class @BeforeClass
Subclass @BeforeClass
Parent class @Before
Subclass @Before
Subclass @Test
Parent class @Test
Subclass @After
Parent class @After
Subclass @AfterClass
Parent class @AfterClass
Demonstration of practical operation 1
Now create a subclass Junit4DemoChildrenTest, Inherit Junit4DemoTest, Then implement the same method as the parent class and run the child class :

Running results :

From the running results, we can see that , The subclass will override the same methods as those in the parent class , Only execute methods in subclasses
Demonstration of practical operation 2
Now change the method name in the subclass , Make it different from the parent method name , Then run the subclass :

Running results :
I am a @BeforeClass, I am the first step I am [email protected], I am the first step I am @Before, Come to me before use case execution. I am [email protected], Come to me before use case execution Children testDemoB I am a [email protected], After the use case is executed, I come to me as @After, After the use case is executed, I come to me as @Before, Come to me before use case execution. I am [email protected], Come to me before use case execution Children testDemoA I am a [email protected], After the use case is executed, I come to me as @After, After the use case is executed, it comes to me java.lang.AssertionError at org.junit.Assert.fail(Assert.java:86) at org.junit.Assert.assertTrue(Assert.java:41) at org.junit.Assert.assertTrue(Assert.java:52) ... at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) I am a @Before, Come to me before use case execution. I am [email protected], Come to me before use case execution Children testDemoC I am a [email protected], After the use case is executed, I come to me as @After, After the use case is executed, I come to me as @Before, Come to me before use case execution. I am [email protected], Come to me before use case execution testDemoA I am a [email protected], After the use case is executed, I come to me as @After, After the use case is executed, it comes to me java.lang.AssertionError at org.junit.Assert.fail(Assert.java:86) at org.junit.Assert.assertTrue(Assert.java:41) at org.junit.Assert.assertTrue(Assert.java:52) ... at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) I am a @Before, Come to me before use case execution. I am [email protected], Come to me before use case execution testDemoB I am a [email protected], After the use case is executed, I come to me as @After, After the use case is executed, I come to me as @Before, Come to me before use case execution. I am [email protected], Come to me before use case execution testDemoC I am a [email protected], After the use case is executed, I come to me as @After, After the use case is executed, I come to me as [email protected], I am the last step I am @AfterClass, I am the last step 2.7 test suite
RunWith
SuiteClasses
class
Demonstration of practical operation
Create a new subclass Junit4DemoChildren2Test, Inherit Junit4DemoTest

Build another test class SuitesTest, Write notes @RunWith(Suite.class), It shows that this is a test suite , Is a collection of multiple test classes , A container ;
Then use annotations @Suite.SuiteClasses To set the test class collection , Set the order of test class execution

Running results :

I am a @Before, Come to me before use case execution. I am [email protected], Come to me before use case execution Children2 testDemoC I am a [email protected], After the use case is executed, I come to me as @After, After the use case is executed, I come to me as @Before, Come to me before use case execution. I am [email protected], Come to me before use case execution Children2 testDemoB I am a [email protected], After the use case is executed, I come to me as @After, After the use case is executed, I come to me as @Before, Come to me before use case execution. I am [email protected], Come to me before use case execution Children2 testDemoA I am a [email protected], After the use case is executed, I come to me as @After, After the use case is executed, it comes to me java.lang.AssertionError at org.junit.Assert.fail(Assert.java:86) at org.junit.Assert.assertTrue(Assert.java:41) ... at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) I am a @Before, Come to me before use case execution. I am [email protected], Come to me before use case execution testDemoA I am a [email protected], After the use case is executed, I come to me as @After, After the use case is executed, it comes to me java.lang.AssertionError at org.junit.Assert.fail(Assert.java:86) at org.junit.Assert.assertTrue(Assert.java:41) ... at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) I am a @Before, Come to me before use case execution. I am [email protected], Come to me before use case execution testDemoB I am a [email protected], After the use case is executed, I come to me as @After, After the use case is executed, I come to me as @Before, Come to me before use case execution. I am [email protected], Come to me before use case execution testDemoC I am a [email protected], After the use case is executed, I come to me as @After, After the use case is executed, I come to me as [email protected], I am the last step I am @AfterClass, I am the last step I am @BeforeClass, I am the first step I am @Before, Come to me before use case execution testDemoA I am a @After, After the use case is executed, it comes to me java.lang.AssertionError at org.junit.Assert.fail(Assert.java:86) at org.junit.Assert.assertTrue(Assert.java:41) ... at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) I am a @Before, Come to me before use case execution testDemoB I am a @After, After the use case is executed, I come to me as @Before, Come to me before use case execution testDemoC I am a @After, After the use case is executed, I come to me as @AfterClass, I am the last step I am @BeforeClass, I am the first step I am [email protected], I am the first step I am @Before, Come to me before use case execution. I am [email protected], Come to me before use case execution Children testDemoB I am a [email protected], After the use case is executed, I come to me as @After, After the use case is executed, I come to me as @Before, Come to me before use case execution. I am [email protected], Come to me before use case execution Children testDemoA I am a [email protected], After the use case is executed, I come to me as @After, After the use case is executed, it comes to me java.lang.AssertionError at org.junit.Assert.fail(Assert.java:86) at org.junit.Assert.assertTrue(Assert.java:41) at org.junit.Assert.assertTrue(Assert.java:52) ... at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) I am a @Before, Come to me before use case execution. I am [email protected], Come to me before use case execution Children testDemoC I am a [email protected], After the use case is executed, I come to me as @After, After the use case is executed, I come to me as @Before, Come to me before use case execution. I am [email protected], Come to me before use case execution testDemoA I am a [email protected], After the use case is executed, I come to me as @After, After the use case is executed, it comes to me java.lang.AssertionError at org.junit.Assert.fail(Assert.java:86) at org.junit.Assert.assertTrue(Assert.java:41) ... at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) I am a @Before, Come to me before use case execution. I am [email protected], Come to me before use case execution testDemoB I am a [email protected], After the use case is executed, I come to me as @After, After the use case is executed, I come to me as @Before, Come to me before use case execution. I am [email protected], Come to me before use case execution testDemoC I am a [email protected], After the use case is executed, I come to me as @After, After the use case is executed, I come to me as [email protected], I am the last step I am @AfterClass, I am the last step I am @BeforeClass, I am the first step I am [email protected], I am the first step From the test results, we can see that after using the suite , The test process is Junit4DemoChildren2Test、Junit4DemoTest、Junit4DemoChildrenTest Sequential execution
2.8 Group test [email protected]
Sometimes we need to group test some specific use cases , It can be used at this time @Category To achieve
In addition, use annotations on the suite execution class :
@RunWith(Categories.class) : Fixed writing , Specify with Category Mode grouping @Categories.IncludeCategory(SlowGroup.class) : Indicate which test groups to execute contain @Categories.ExcludeCategory(FastGroup.class) : Indicate which test groups to execute do not contain @Suite.SuiteClasses({ : Indicate the test class to be executed TestDemo.class})@CategoryGrouping needs to be given a label , As a class or interface , Create an interface hereSlowGroupandFastGrouppublic interface FastGroup {}public interface SlowGroup {}The use cases are grouped into
SlowGroup、FastGroupandSlowGroup+FastGroup

To specify
SlowGroupGroup test execution ,FastGroupGroup tests are not performed :

test result :

Indicate only
SlowGroupGroup test execution

test result :

Only indicate that the group not executed is
FastGroup

test result :

2.9 A parameterized @Paramterized
Sometimes we need to pass in test data , And the data may be multiple groups , At this time, you need to use parameterization to pass in multiple sets of data for testing
Junit4 Parameterization of is a little troublesome :
1) First add comments to the class name @RunWith(Parameterized.class) Indicates that you want to run with parameterization

2) With notes @Parameterized.Parameters To set the data source

3) Finally, use notes @Parameterized.Parameter To specify the parameters corresponding to the data source data

4) The overview

test result :

As you can see from the test results 3 Group parameters are passed into the method respectively , Methods are executed once each , Complete parametric testing
3、 summary - The sequence of test cases
The order between test cases
test fixtures The order of
Inheritance order
The order between kits
Reference document link
JUnit4 Unit test framework [https://junit.org/junit4/]
JUnit5 Unit test framework [https://junit.org/junit5/]
边栏推荐
- Entropy - conditional entropy - joint entropy - mutual information - cross entropy
- VMware16安装Win11虚拟机(最全步骤+踩坑)
- Cloud Native Landing Practice Using rainbond for extension dimension information
- 4个技巧告诉你,如何使用SMS促进业务销售?
- Memory Limit Exceeded
- sqlserver SQL Server Management Studio和Transact-SQL创建账户、创建访问指定数据库的只读用户
- Electron 入门
- JS string interception method summary
- 期货怎么开户安全些?现在哪些期货公司靠谱些?
- How to open a futures account safely? Which futures companies are more reliable now?
猜你喜欢

German agbb VOC hazardous substances test

PC端微信多开

拓維信息使用 Rainbond 的雲原生落地實踐

如何使用物联网低代码平台进行服务管理?

嵌入式软件开发新趋势:DevOps

Swin-Transformer(2021-08)

Swin-transformer --relative positional Bias

Some interesting modules

France a+ France VOC label highest environmental protection level

Sqlserver SQL Server Management Studio and transact SQL create accounts and create read-only users to access the specified database
随机推荐
3.10 haas506 2.0 development tutorial example TFT
Neon optimization 2: arm optimization high frequency Instruction Summary
torch stack() meshgrid()
教你Selenium 测试用例编写
Practical application of "experience" crawler in work "theory"
企业选型作业上常犯的一个错误
Business Intelligence BI and business management decision-making thinking 4: business cost analysis
Compare the audio librosa library with the Mel spectrogram in the torchaudio library
Full recharge, im+rtc+x full communication service "feedback season" starts
Is it safe to open an account for goucai? Is it reliable?
MySQL modify data type_ MySQL modify field type [easy to understand]
torch. roll
音频 librosa 库 与 torchaudio 库中 的 Mel- spectrogram 进行对比
20200525-生物技术-四川师范大学自考生物技术(本科)考试计划.txt
基于 actix、async-graphql、rbatis、pgsql/mysql 构建 GraphQL 服务(4)-变更服务
Swin-transformer --relative positional Bias
如何利用 xUnit 框架对测试用例进行维护?
DTD modeling
MySQL事务并发问题和MVCC机制
MySQL recursion