当前位置:网站首页>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 :

  1. Test Fixture The background and significance of ,

  2. Test Fixture The concept and characteristics of ,

  3. 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 .

原网站

版权声明
本文为[Wonderful binary]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207042048259864.html