当前位置:网站首页>GTEST from ignorance to proficient use (2) what is test fixture
GTEST from ignorance to proficient use (2) what is test fixture
2022-07-04 21:42:00 【Wonderful binary】
Introducing gtest A few test terms were introduced before .
Fixture Is an important concept in software testing . Many people feel vague about this concept , One of the reasons is that so far it has no unified Chinese Translation . Some people call it testing firmware , Some people call it a test device , Others call it a test fixture . Personally, I have no special preference for translation , Therefore, it is directly called Fixture.
that , What exactly is Fixture Well ? actually ,Fixture The concept comes from Electronics , It refers to the auxiliary devices required for testing electronic components . Later, this concept was introduced into the field of software testing , It refers to the system that can provide software testing , Environmental Science , State and other preconditions .
in my opinion ,Fixture It's an abstract thing , Its abstraction is embodied in two aspects .
One side ,Fixture Of Function is abstract . The preconditions of test cases are ever-changing ,Fixture The specific functions of are also changing . for example , A test case for reading the contents of a file , The precondition is an open file , here Fixture The function of is to open the file ; And a test HTTP Interface test cases , The precondition is a pair of running HTTP The server / client , here Fixture The function of is to create and start HTTP The server / client .
On the other hand ,Fixture Of Form is also abstract . Even the same test case , In different test environments ,Fixture There may be different forms . for example , Test cases for reading file contents ,Fixture The function of is to open the file , However, there are many possibilities for its specific implementation . It can be an operation of double clicking the file icon , It can also be a line Linux command , It can also be a paragraph Python Script .
because Fixture It is abstract in both function and form , Therefore, it is not difficult to understand why many people are right Fixture There is a sense of incomprehension . here , According to the discussion above , We give Fixture Next my own definition : Fixture In the process of software testing , Actions or scripts that create preconditions on which test cases depend .
What needs to be added is ,“ Everything has a beginning and an end ”,Fixture It's the same thing . The environment needs to be prepared before executing the test case , Then the environment needs to be restored after the use case is executed . therefore ,Fixture There is also an implied function , That is after the test , Restore the test environment , Return to the initial state .
Talking about here , You may still be right Fixture I don't understand the meaning of . Prepare and destroy the environment , Initialize and clear the state , Why not directly complete the test case itself ?" Out of thin air " Make a Fixture, Will you complicate simple problems ?
This is a good question . Of course, these operations can be completed by the test case itself . However ,“ Can you " It's not the point , The key is " Ok or not ”.
Software testing has a feature , That is, for the same user scenario , Many test cases with the same preconditions but different test steps are often designed ( test suite Test Suite The concept of comes from this ). here , If each use case maintains its own preconditions , There is not only waste , And it is difficult to maintain consistency .
A better way is " Merge items of the same kind ". When multiple use cases have the same preconditions , We can extract the preconditions , As Fixture, Called by each use case . This approach is efficient , Easy to maintain , Consistency is also better .
From a design pattern point of view , This practice abides by two principles . One is " Single data source "(Single Source of Truth) principle , That is, to complete a certain function Fixture There is and only one ; Two is " Separation of concerns "(Separation of Concerns) principle , That is to let Fixture Be responsible for the preparation and destruction of the test environment and status , Let the test case be responsible for the work of the test subject , Both sides perform their respective duties , Responsibility details .
thus , We introduce... Theoretically Fixture The principle and meaning of . Next , Let's look at it in practice ,Fixture How to apply it to engineering practice .
One of the most common implementations Fixture The way is Setup/Teardown. It widely exists in various mainstream automated testing frameworks , for example C++ Google Test, Java JUnit, Python Unittest, Robot Framework etc. .
Their common feature is based on Setup Functions or methods , Complete the preparation of the test environment and state ; be based on Teardown Functions or methods , Complete the destruction of the test environment and state .
Based on Setup/Teardown In the test framework of , The general process of test cases includes four parts : Setup -> Execute use cases , Interact with the software under test -> The verification results , Compare with expectations -> Teardown, Go back to the original state .
We can define different levels Setup/Teardown Method , To achieve a different range of Fixture share . for example , Defined in use cases Setup, Call only before this use case is executed ; stay Suite As defined in Setup, Only in Suite Call before execution , stay Suite All test cases in are no longer called when executing .
be based on Setup/Teardown Of Fixture The implementation method exists for a long time , Wide application ,“ High status in the Jianghu ”. however , In recent years , There is another Fixture Realization way , Gradually known . It is Pytest Fixture.
Pytest yes Python Unit test framework " Three swordsmen " One of . And Python Unitest, Python Nose The difference is ,Pytest Not only support unit testing , It also supports various complex functional tests ,UI Testing and end-to-end testing .Pytest" Wide application spectrum " One of the reasons , It uses a new Fixture Realization way .
stay Pytest in , be based on @pytest.fixture Decorator , arbitrarily Python Functions can be registered as Fixture.Fixture Your name is Python Function name . The test case ( Test function ) By way of Fixture How the name is declared in the input parameter table , To choose and use Fixture.
take Fixture Defined as a function , Can achieve Fixture The modular .Fixture You can call various libraries and modules , It can also be Fixture Nested calls in the form of input parameter declarations . And Setup/Teardown The difference is ,Pytest Fixture Not only in Case and Suite Range sharing , You can also do it in Package and Session Range sharing .
Call... As an input parameter declaration Fixture Another advantage of is that you can achieve fine-grained Fixture Choose and combine . Each test case can integrate its dependent Fixture Put it in the input parameter table , And will not rely on Fixture To eliminate . Repeated Fixture The combination can be encapsulated into a new Fixture.Setup/Teardown There is no such flexibility in the way .
in addition ,Pytest Fixture Support parameterization . For the same test case , Configure according to different parameters , Different... Can be invoked dynamically during the execution of use cases Fixture. such , It can realize the requirement that a test case covers multiple scenarios . Based on parameterization Fixture, Data driven testing is easier to implement , And better maintenance .
You can see , With the traditional Setup/Teardown Compared to model ,Pytest Fixture Mode is more convenient to use , And more powerful . Personally think that ,Pytest Fixture The future prospect of the model is worth looking forward to .
To sum up , This paper introduces :
Test Fixture The background and significance of ,
Test Fixture The concept and characteristics of ,
Two kinds of Test Fixture Implementation pattern : Setup/Teardown and Pytest Fixture Their respective meanings and characteristics .
I hope that by reading this article , Everyone to "Test Fixture" No longer strange or confused , This knowledge and understanding can go to a higher level .
边栏推荐
- Jerry's ad series MIDI function description [chapter]
- Huawei ENSP simulator realizes communication security (switch)
- Use of class methods and class variables
- Cadeus has never stopped innovating. Decentralized edge rendering technology makes the metauniverse no longer far away
- How was MP3 born?
- The video sound of station B is very low - solution
- TCP shakes hands three times and waves four times. Do you really understand?
- Shutter textfield example
- WGCNA analysis basic tutorial summary
- 杰理之AD 系列 MIDI 功能说明【篇】
猜你喜欢
Keep on fighting! The city chain technology digital summit was grandly held in Chongqing
【C語言】符號的深度理解
杰理之AD 系列 MIDI 功能说明【篇】
Huawei ENSP simulator layer 3 switch
Stealing others' vulnerability reports and selling them into sidelines, and the vulnerability reward platform gives rise to "insiders"
Application practice | Shuhai supply chain construction of data center based on Apache Doris
【C语言】符号的深度理解
Lambdaquerywrapper usage
How was MP3 born?
Difference between ApplicationContext and beanfactory (MS)
随机推荐
SolidWorks工程图添加材料明细表的操作
Redis pipeline
Monitor the shuttle return button
Three or two things about the actual combat of OMS system
[wechat applet] collaborative work and release
Go language loop statement (3 in Lesson 10)
B站视频 声音很小——解决办法
Difference between ApplicationContext and beanfactory (MS)
Flutter TextField示例
每日一题-LeetCode556-下一个更大元素III-字符串-双指针-next_permutation
Flink1.13 SQL basic syntax (I) DDL, DML
How was MP3 born?
TCP三次握手,四次挥手,你真的了解吗?
哈希表(Hash Tabel)
2021 CCPC Harbin I. power and zero (binary + thinking)
maya灯建模
redis事务
为什么说不变模式可以提高性能
Delphi SOAP WebService 服务器端多个 SoapDataModule 实现相同的接口方法,接口继承
【微信小程序】协同工作与发布