当前位置:网站首页>Integration test practice (1) theoretical basis
Integration test practice (1) theoretical basis
2022-07-03 06:43:00 【Bright in snow】
How procedural apes view self-test ? Why pay attention to testing ? How to test ? This paper summarizes the relevant issues , Much of it comes from the geek time column 「 Programmer's testing class 」. The next article focuses on the practice of integration testing .
1. Why write tests ?
The key reason : Software development is becoming more and more complex . Testing allows us to move steadily forward in increasingly complex software development .
When writing new functions , Testing can verify the correctness of our code , Let's have stable modules .
Testing can help us continue to return in the long-term process , Let each step go more steadily . Isolate change , Write stable code step by step .
2. Why should programmers write tests ?
Programmer's test VS Tester's test
Programmers focus on white box testing , And the tester is black box testing .
The starting point of programmers is to realize , The starting point of testers is business . As a programmer , We will spend more time thinking about technology implementation , Our training intensity in finding problems is far from enough . therefore , People often say that , Don't challenge others' eating skills with your hobbies .
Every time programmers think a little more , The software quality can be improved a little more .
The testers didn't give full play . Only programmers do their own tests , Only in this way can testers be freed from daily trivial verification work , To do more valuable tests .
3. Unit test best practices
a key : Code and tests are written together
Ensure test quality : The whole function code has been written , Then write the test , It's hard for you to remember all the details when writing this pile of code , At this time, the supplementary tests are not very helpful to improve the code quality .
The granularity of work should be small : The granularity of writing all the functions is too large . Write tests for a big task , Is a very difficult thing , This is also an important factor that many people find it difficult to write tests .
Minimize constraints on implementation details :mock when , Tend to use broader constraints . To some extent, this will reduce the impact of refactoring code in the future .
4. Make sure you write your own code 100% Test coverage
Have a testable design , Make sure your code is completely controllable
Test and code are written synchronously
Isolate hard to test code : For code that cannot be tested to a third party , Use a thin isolation layer to isolate the code , Exclude the isolation layer from the build script .
5. Integration testing
even 100% Unit testing of can't solve all the problems . An important reason is , When we write each unit, we assume that these units can work well with each other , But is this assumption necessarily true ? The answer is not necessarily . In addition to ensuring the correctness of the unit , We also need to ensure that the collaboration between units is also correct . As opposed to unit testing, it only focuses on unit behavior , Integration testing focuses on the performance of multiple components working together .
5.1 The significance of integration testing
To ensure the correctness of collaboration between components ;
You need to use the same assembly process as the product code ;
You can take the tested stable components as the basis .
5.2 Two integration issues and best practices
5.2.1 Integration between codes
1) Choose a path for task execution , Integrate the components used on the path for testing .
After the unit test is completed , It means that every component has been tested . therefore , The focus of integration testing is no longer the collaborative testing between components .
2) Integrate the framework , Do a complete integration test .
Integrate as much as possible , As close to the real scene as possible . If we can integrate the whole framework , These things can also be verified .
3) Integration logic between components , Unit tests are not required
This part of code is actually just a simple encapsulation of business logic , The main work is information forwarding , This will be a very thin layer . For practical reasons , Here we use integration testing instead of unit testing , Simplify test writing .
5.2.2 Integration of code with external components
1) You can't modify the code for the needs of testing
If you really want to modify , Maybe what should be modified is the design , Not just code . There can be no special logic in business code just to run through a test case .
6. Test the proportioning model
6.1 Ice cream cone model
The starting point of ice cream cone is to consider the coverage of a single test , Just some system testing , It is enough to cover most situations of the system . Of course , For those scenarios that cannot be covered by system testing, low-level test cooperation is required , such as , Integration testing and unit testing . In the ice cream cone model , The main force is high-level testing , Low level testing is just a supplement to high-level testing .
6.2 Test the pyramid model
6.3 Best practices
The new project : Use the test pyramid model , Write tests layer by layer . Each function is completed , Code and tests are always written synchronously , Code is always validated , So we can move forward steadily .
Legacy projects : Start with system testing , Just write some high-level tests , It can cover most functions of the system , Can quickly establish a safety net , Belong to “ Less investment, quick results ” How to do it . After having the bottom line of a safety net , We still have to move towards the test pyramid , Take unit testing as the basis of the whole .
7. Write test best practices
7.1 The goal of our testing is our code , Not external dependencies
Due to other libraries 「JDK、 Two party Library 、 Three party Library 、RPC etc. 」 Cause problems that are difficult to test , We can make thin layers of packaging , then , Ignore it in the coverage check . Encapsulation and neglect , Be short of one cannot .
7.2 Write testable code
Make your code conform to software design principles .
Write composable code .
Don't create objects inside components , Component assembly uses dependency injection .
Constructor Injection VS Field injection
8. Best practices for transforming legacy systems
Legacy systems are those that have not been tested , Writing tests for legacy systems is the process of getting a system back to normal .
It is difficult to write tests for a legacy system completely . A practical improvement strategy is , Where to move , Change where .
The key to transforming legacy systems is decoupling . For heavily coupled objects , Extraction method , Move to an encapsulated class . Write composable code .
9. unit testing 、 Integration testing 、 System test comparison
9.1 unit testing
describe : Developers complete , Write tests for units , The particle size should be as small as possible , It's usually a method 、 One class .
The goal is : Ensure that every part is stable .
Number of cases : most
9.2 Integration testing
describe : Usually developers complete , Integrate multiple units for testing , Generally, complete functional modules are tested . General integration Spring Containers 、 In-memory database 、 has Mock External dependence of .
The goal is : Ensure that the functional modules are stable . Do not pay attention to whether the parts in the module are stable 「 This is the work of unit testing 」.
Number of cases : Less
9.3 The system test
describe : The tester completes , Integrate the whole system completely , Can automate the execution of . Don't pay attention to implementation details , Pay attention to whether the requirements are realized .
practice : Test the whole application in the test environment , Don't do external dependencies Mock.
The goal is : Ensure that the complete business is stable . Theoretically, there is no need to pay too much attention to details 「 unit testing 、 Integration testing theoretically does these , But in fact, system testing still needs to pay attention to details 」
Number of cases : Less
10. Reference material
- Geek time - special column - Programmer's testing class
边栏推荐
- UTC time, GMT time, CST time
- Journal quotidien des questions (11)
- C2338 Cannot format an argument. To make type T formattable provide a formatter<T> specialization:
- Difference between shortest path and minimum spanning tree
- Mysql
- New knowledge! The virtual machine network card causes your DNS resolution to slow down
- SSH link remote server and local display of remote graphical interface
- Mysql database
- [untitled] 5 self use history
- [LeetCode]404. 左叶子之和
猜你喜欢
2022 cisp-pte (III) command execution
SQL实现将多行记录合并成一行
【开源项目推荐-ColugoMum】这群本科生基于国产深度学习框架PaddlePadddle开源了零售行业解决方案
每日刷題記錄 (十一)
Operation principle of lua on C: Foundation
JMeter linked database
Paper notes vsalm literature review "a comprehensive survey of visual slam algorithms"
Ruoyi interface permission verification
scroll-view指定滚动元素的起始位置
Local rviz call and display of remote rostopic
随机推荐
2022 - 06 - 23 vgmp - OSPF - Inter - Domain Security Policy - nat Policy (Update)
VMware virtual machine C disk expansion
熊市里的大机构压力倍增,灰度、Tether、微策略等巨鲸会不会成为'巨雷'?
Asynchronous programming: async/await in asp Net
Daily question brushing record (11)
Pdf files can only print out the first page
Cannot get value with @value, null
Scripy learning
2022-06-23 VGMP-OSPF-域间安全策略-NAT策略(更新中)
Unittest attempt
Selenium - 改变窗口大小,不同机型呈现的宽高长度会不一样
IC_EDA_ALL虚拟机(丰富版):questasim、vivado、vcs、verdi、dc、pt、spyglass、icc2、synplify、INCISIVE、IC617、MMSIM、工艺库
Difference between shortest path and minimum spanning tree
How matlab modifies default settings
Understand software testing
How does the insurance company check hypertension?
Mysql database
HMS core helps baby bus show high-quality children's digital content to global developers
Scroll view specifies the starting position of the scrolling element
[LeetCode]404. 左叶子之和