当前位置:网站首页>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 .
边栏推荐
- Daily question -leetcode1200- minimum absolute difference - array - sort
- Drop down selection of Ehlib database records
- For MySQL= No data equal to null can be found. Solution
- 解决异步接口慢导致的数据错乱问题
- Huawei ENSP simulator configures DHCP for router
- Case sharing | integrated construction of data operation and maintenance in the financial industry
- flink1.13 sql基础语法(一)DDL、DML
- 杰理之AD 系列 MIDI 功能说明【篇】
- Redis cache
- How was MP3 born?
猜你喜欢

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

Operation of adding material schedule in SolidWorks drawing

ArcGIS 10.2.2 | solution to the failure of ArcGIS license server to start

杰理之AD 系列 MIDI 功能说明【篇】

CAD中能显示打印不显示
![[weekly translation go] how to code in go series articles are online!!](/img/bf/77253c87bfa1512f4b8d3d8f7ebe80.png)
[weekly translation go] how to code in go series articles are online!!

开源之夏专访|Apache IoTDB社区 新晋Committer谢其骏

torch. Tensor and torch The difference between tensor

How was MP3 born?

奋斗正当时,城链科技战略峰会广州站圆满召开
随机推荐
Numpy vstack and column_ stack
CloudCompare&Open3D DBSCAN聚类(非插件式)
股票开户佣金最低多少,炒股开户佣金最低网上开户安全吗
Methods of improving machine vision system
Jerry added the process of turning off the touch module before turning it off [chapter]
Huawei ENSP simulator realizes communication security (switch)
2021 CCPC Harbin I. power and zero (binary + thinking)
Analyzing the maker space contained in steam Education
Configuration of DNS server of Huawei ENSP simulator
Can be displayed in CAD but not displayed in print
【LeetCode】17、电话号码的字母组合
面试官:说说XSS攻击是什么?
Kubedm initialization error: [error cri]: container runtime is not running
[leetcode] 17. Letter combination of telephone number
Delphi soap WebService server-side multiple soapdatamodules implement the same interface method, interface inheritance
How to remove the black dot in front of the title in word document
迈动互联中标北京人寿保险
Huawei ENSP simulator configures DHCP for router
【活动早知道】LiveVideoStack近期活动一览
Redis cache