Catalog : Reading guide
Preface
Unit test framework
1)JUnit edit
2)TestNG
3)GoogleTest
4)pytest
5)unittest
unit testing Mock Tools
6)JMockit
Unit test coverage tool
7)JCoCo
8)gcov、lcov、gcovr
9)Coverage.py
10)EvoSuite
Conclusion
In the application , A unit is the smallest testable part of software with one or more inputs and a single output . Unit testing is a method of testing software code units , It usually includes one or two inputs , Produce an output . Unit testing focuses on the functional correctness of independent modules , The aim is to ensure that each unit operates as expected .
Unit tests are required , Developers need to write test code . There are two types of unit tests, manual and automated , Automation is often the preferred method , It can save a lot of time and energy for developers .
Unit test is the largest test type in the automated test pyramid model , Unit testing is very important to ensure the quality of software products , Unit tests can :
Now let's summarize what we have done for Java、C++ and Python Language unit testing , Popular testing tools , They include not only unit test frameworks , It also includes Mock Tools 、 Code coverage tools , as well as Two intelligent unit test case automatic generation tools :
1、Junit
2、TestNG
3、GoogleTest
4、pytest
5、unittest
6、Jmockit
7、JaCoCo
8、gcov、lcov、gcovr
9、Coverage.py
10、EvoSuite
11、Diffblue Cover
The current mature unit testing frameworks include :JUnit、TestNG、gtest、pytest、unittest.
JUnit One is for Java Open source unit testing framework designed by programming language , from Kent Beck and Erich Gamma establish , It is one of the unit test framework families , These frameworks are collectively referred to as xUnit,JUnit yes xUnit The most successful one in the family .JUnit It has its own Junit Expand the ecosystem , Most of the Java The development environment of has been integrated JUnit As a tool for unit testing .JUnit The latest version of is JUnit 5, It is no longer a single JAR package , But by the JUnit platform( platform )、JUnit Jupiter and JUnit Vintage this 3 Part of it is made up of .
Official website address :https://junit.org/junit5/
TestNG Is another for Java Open source unit testing framework designed by programming language , It's a recipient JUnit and NUnit Inspired testing framework , But it introduces some new features , Make it stronger 、 Easier to use , for example :
The core feature is multithreaded test execution , Test whether the code is multithread safe ;
Provide annotation support ;
Support data-driven testing ( Use @DataProvider);
Support parametric testing ;
Powerful execution model ( No more TestSuite);
Support various tools and plug-ins (Eclipse, IDEA, Maven etc. …);
The embedded BeanShell For more flexibility ;
Default for runtime and logging JDK function ( No dependency ).
Official website address :https://testng.org/
GoogleTest It's a cross platform (Liunx、Mac OS X、Windows 、Cygwin 、Windows CE and Symbian ) C++ Unit test framework , from google company , For writing on different platforms C++ Developed for testing . It provides rich assertions 、 Fatal and non fatal judgments 、 A parameterized 、” Death test ” wait . for example :
The test case itself is a exe engineering , After compilation, you can directly run , Very convenient .
Writing test cases becomes very simple ( Use simple macros such as TEST), Let's focus more on test case design .
Macros that provide powerful and rich assertions , Used to check various checkpoints .
It improves the rich command line parameters to set a series of settings for script running .
Open source code address :https://github.com/google/googletest
pytest Is a very mature full-featured support Python Language unit automation testing framework . Simple and flexible , Easy to use , Support parameterization ; Support simple unit testing and complex functional testing , It can also be used to make selenium/appnium Wait for automated testing , And interface automation testing (pytest Integrate requests).
Official website address :http://pytest.org/
unittest yes Python It comes with a unit testing framework , No installation required , Easy to use , Introduction package import unittest You can use .unittest The unit test framework acts as python The standard library of , It is the foundation of other unit testing frameworks .
Official website address :http://docs.python.org/zh-cn/3/library/unittest.html
In unit testing , The classes to be tested have many dependencies , These dependent classes or resources will have dependencies , As a result, the construction cannot be completed in the unit test code . therefore , We need to adopt Mock Technology isolates dependent objects , That is, simulate the classes or resources that need to be built , Provided to the object to be tested .
JMockit Is a Java Open source language unit testing Mock Tools , Contains tools and API aggregate .Jmockit You can talk to junit and TestNG Write unit tests in conjunction with .
JMockit Support class level integration mock And partial method rewriting , And instance level as a whole mock And part mock, Sure mock Static methods 、 Private variables and local methods .
This tool also has the function of statistical unit test code coverage , Provides three types of code coverage , Such as line coverage 、 Path coverage and data coverage .
Official website address :http://jmockit.org/
Code coverage tools are also needed in unit tests . Code coverage is a measure of the code covered during unit testing . The code coverage statistics tool is used to find the code that is not covered by the test , Improve the coverage of unit tests . in addition , Such tools can also be used to build code call relationships , Accurately determine the scope of regression test , Avoid the waste of testing resources caused by full regression .
Jacoco Is an open source free Java Code coverage tools , from EclEmma establish , Its use method is very flexible , Can be embedded into Ant、Maven in ; It can be used as Eclipse plug-in unit , You can use it Java Agent Technical monitoring Java Procedures, etc . Many third-party tools provide access to JaCoCo Integration of , Such as sonar、Jenkins etc. .
JaCoCo It contains coverage counters with multiple scales :
Instruction level overrides (Instructions,C0coverage)
Branch (Branches,C1coverage)
Cyclomatic complexity (CyclomaticComplexity)
Line overlay (Lines)
Methods cover (non-abstract methods)
Class coverage (classes)
Official website address :https://www.jacoco.org/jacoco/
gcov、lcov、gcovr All support. C/C++ Tools for language code coverage .
gcov By gcc Built in code coverage generation tool , It can be very convenient and GCC The compiler is used in conjunction with , Usually , Direct installation gcc Tool chain , It also includes gcov Command line tools .gcov The result is in text form , And different source files need to be executed one by one gcov command , It is inconvenient for large projects , It's time to lcov.
lcov yes gcov The graphical front end of the tool , Collection of multiple source files gcov data , Generate... That describes coverage HTML page . The generated results will include an overview page , Easy to browse .
Use in general scenarios gcov and lcov It can meet the requirements of code coverage acquisition and display ,lcov and genhtml Match generated HTML The report is detailed , Simple and intuitive , Line coverage 、 Branch coverage has , however HTML File in common continuous integration tools ( such as Jenkins、gitlab-ci) Cannot be integrated in , So we need other tools for continuous integrated presentation of coverage results .gcovr It's about C/C++ Code coverage and support in multiple ways ( Including list mode 、XML File mode 、HTML Web page mode, etc ) The tools shown , and XML The file happens to be parsed by the continuous integration tool .
lcov Download address :https://github.com/linux-test-project/lcov
gcovr Download address :https://github.com/gcovr/gcovr
Coverage.py It's a measure Python Tools for language code coverage .Coverage.py You can specify which source files to analyze through the configuration file , By analyzing the source code , Find code that is not covered by the test .
Official website address :https://coverage.readthedocs.io
Intelligent unit test case automatic generation tool
There is no doubt about the importance of unit testing , But it is not well implemented in many enterprises , A very important reason is that developers are not willing to spend their time writing unit test code . Now , There are two recommended intelligent tools that can automatically generate unit test cases , Help improve unit test coverage , Improve the R & D efficiency of enterprises .
EvoSuite It's from England Sheffield( Shaffield ) An open source tool jointly developed by other universities , Used to automatically generate test case sets , The generated test cases are consistent with Junit Standards for , But directly in Junit Run in , And got Google and Yourkit Support for . By using this automatic testing tool, the development efficiency of testers can be greatly improved under the premise of ensuring code coverage . But only auxiliary testing , It can't completely replace labor , Whether the test case is correct or not needs manual judgment .
Official website address :https://www.evosuite.org/
Open source code address :https://github.com/EvoSuite/evosuite
Diffblue Cover Is an automated unit test writing tool , Through analysis Java The application writes unit tests that reflect the current behavior , Improve test coverage , And help developers find regression defects in future code changes .
Plug in open source community version address :https://plugins.jetbrains.com/plugin/14946-diffblue-cover--community-edition/versions
The following is a very high-quality learning resource , For doing 【 software test 】 For our friends, it should be the most comprehensive and complete war preparation warehouse , This warehouse also accompanied me through the most difficult journey , I hope it can help you !
The above software test data can be obtained by private mail for free
Here's another one 216 Page software testing engineer interview guide document information . And the corresponding video learning tutorial is free to share !, The information includes basic knowledge 、Linux necessary 、Shell、 The principles of the Internet 、Mysql database 、 Special topic of bag capturing tools 、 Interface testing tool 、 Test advanced -Python Programming 、Web automated testing 、APP automated testing 、 Interface automation testing 、 Testing advanced continuous integration 、 Test architecture development test framework 、 Performance testing 、 Safety test, etc. .
Learn not to fight alone , It's best to keep warm , Achieve each other and grow together , The effect of mass effect is very powerful , Let's learn together , Punch in together , Will be more motivated to learn , And you can stick to it .
Finally, one more word , If you want to take testing as your long-term career goal , You need to keep learning all the time , Make yourself competitive , No matter how many years you work now , Just act , You already have an advantage , Well, that's it , Wish you all 2022 I can get a promotion and a raise every year , If you are not employed, you will get the of your favorite company as soon as possible offer, Everything goes well .
Welcome to leave a message , Or follow my column and communicate with me .
Python One click to collect al