当前位置:网站首页>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 .
边栏推荐
- 一文掌握数仓中auto analyze的使用
- EhLib 数据库记录的下拉选择
- 每日一题-LeetCode1200-最小绝对差-数组-排序
- Redis03 - network configuration and heartbeat mechanism of redis
- Analysis of maker education technology in the Internet Era
- redis管道
- redis布隆过滤器
- 【LeetCode】17、电话号码的字母组合
- Minidom module writes and parses XML
- Jerry's ad series MIDI function description [chapter]
猜你喜欢

redis03——Redis的网络配置与心跳机制

How was MP3 born?

奋斗正当时,城链科技战略峰会广州站圆满召开

Keep on fighting! The city chain technology digital summit was grandly held in Chongqing

How was MP3 born?

超详细教程,一文入门Istio架构原理及实战应用
![[early knowledge of activities] list of recent activities of livevideostack](/img/14/d2cdae45a18a5bba7ee1ffab903af2.jpg)
[early knowledge of activities] list of recent activities of livevideostack

Huawei ENSP simulator enables devices of multiple routers to access each other

Master the use of auto analyze in data warehouse

杰理之增加进关机前把触摸模块关闭流程【篇】
随机推荐
At the right time, the Guangzhou station of the city chain science and Technology Strategy Summit was successfully held
redis发布订阅的使用
杰理之AD 系列 MIDI 功能说明【篇】
redis缓存
历史最全混合专家(MOE)模型相关精选论文、系统、应用整理分享
股票开户佣金最低多少,炒股开户佣金最低网上开户安全吗
Golang interview finishing three resumes how to write
__ init__ () missing 2 required positive arguments
Configuration of DNS server of Huawei ENSP simulator
Huawei ENSP simulator layer 3 switch
Jerry's ad series MIDI function description [chapter]
改善机器视觉系统的方法
CAD中能显示打印不显示
杰理之AD 系列 MIDI 功能说明【篇】
Go语言循环语句(第10课中3)
redis RDB AOF
Huawei ENSP simulator enables devices of multiple routers to access each other
Redis 排查大 key 的3种方法,优化必备
ArcGIS 10.2.2 | solution to the failure of ArcGIS license server to start
[ 每周译Go ] 《How to Code in Go》系列文章上线了!!