当前位置:网站首页>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 .
边栏推荐
- Flink1.13 SQL basic syntax (I) DDL, DML
- 案例分享|金融业数据运营运维一体化建设
- Delphi soap WebService server-side multiple soapdatamodules implement the same interface method, interface inheritance
- 更强的 JsonPath 兼容性及性能测试之2022版(Snack3,Fastjson2,jayway.jsonpath)
- Y56. Chapter III kubernetes from entry to proficiency -- business image version upgrade and rollback (29)
- Flutter TextField示例
- Jerry's ad series MIDI function description [chapter]
- Golang面试整理 三 简历如何书写
- Arcgis 10.2.2 | arcgis license server无法启动的解决办法
- 旋变串判断
猜你喜欢
Huawei ENSP simulator realizes communication security (switch)
A quick start to fastdfs takes you three minutes to upload and download files to the ECS
MP3是如何诞生的?
Billions of citizens' information has been leaked! Is there any "rescue" for data security on the public cloud?
How was MP3 born?
输入的查询SQL语句,是如何执行的?
奋斗正当时,城链科技战略峰会广州站圆满召开
Huawei ENSP simulator configures DHCP for router
超详细教程,一文入门Istio架构原理及实战应用
【LeetCode】17、电话号码的字母组合
随机推荐
2021 CCPC 哈尔滨 I. Power and Zero(二进制 + 思维)
Go language loop statement (3 in Lesson 10)
解析steam教育中蕴含的众创空间
Redis pipeline
Maidong Internet won the bid of Beijing life insurance
Open3D 曲面法向量计算
Caduceus从未停止创新,去中心化边缘渲染技术让元宇宙不再遥远
redis RDB AOF
B站视频 声音很小——解决办法
【公开课预告】:视频质量评价基础与实践
Three or two things about the actual combat of OMS system
Redis03 - network configuration and heartbeat mechanism of redis
Monitor the shuttle return button
超详细教程,一文入门Istio架构原理及实战应用
Jerry's ad series MIDI function description [chapter]
Maya lamp modeling
Flink1.13 SQL basic syntax (I) DDL, DML
Cadeus has never stopped innovating. Decentralized edge rendering technology makes the metauniverse no longer far away
Master the use of auto analyze in data warehouse
[buuctf.reverse] 151_ [FlareOn6]DnsChess