当前位置:网站首页>Getting started with pytest ----- test case pre post, firmware

Getting started with pytest ----- test case pre post, firmware

2022-07-06 17:49:00 Peng Yuyan in the testing industry

1、 Back and forth

The so-called pre post , Is similar to unittest In the framework setup and teardown—— Open the browser before executing the use case ( In front of ), Close the browser after execution ( After ). for example :

The printed part of the code above is setupsetup_classteardownteardown_class Description of each method
But if there is another file , These operations are also required 【 such as ui To automate each use case, open the browser and execute 】 We need to encapsulate these common methods . notes : Public Library , Customarily, it will be defined as common

We have a new one called common Of package, Then create... In it common_util file , The new class CommonUtil Cut public methods into this class

Then other classes only need to inherit this class 【python Basic knowledge of 】

The above is what we learn unittest Time thinking . But now if there is The new demand : Only one use case requires preconditions , The rest don't need . What should I do at this time ? It seems that this method of inheritance is not so flexible .

This leads to fixture Come on Realize partial pre post

grammar :@pytest.fixture()

Parameters :

scope -- Scope   

        function: Before and after the function

        class: class After and before execution

        package/session: The whole project session After and before execution

autouse -- Automatic execution

         Default :False; If instead True, You don't need to call , In the corresponding scope, it will automatically execute

...

Let's start with a popular example :

hold CommonUtil The code inside is commented out , direct pass, Let it no longer take effect

Then modify the test file :

After execution, you can see , Only test_py Method is called exe_sql Method .

Add the second parameter autouse The value is True, After execution, you can see that all use cases reference exe_sql Method

up to now , All are “ Before ” perform ,“ after ” How to do it?

after ” Use yield

Execution results :

notes : In a file There can be multiple fixture, You can add as many as you need

however fixture This firmware Only effective in the current document , Other documents will not take effect , If other files are to take effect, configure conftest.py The file .

And there are many other parameters , We'll talk about these tomorrow .

The following is the supporting information , For doing 【 software test 】 For our friends, it should be the most comprehensive and complete war preparation warehouse , This warehouse also accompanied me through the most difficult journey , I hope it can help you !å¨è¿éæå¥å¾çæè¿°

Last : It can be in the official account : Programmer Xiaohao  ! Get a free copy of 216 Page software testing engineer interview guide document information . And the corresponding video learning tutorial is free to share !, It includes basic knowledge 、Linux necessary 、Shell、 The principles of the Internet 、Mysql database 、 Special topic of bag capturing tools 、 Interface testing tool 、 Test advanced -Python Programming 、Web automated testing 、APP automated testing 、 Interface automation testing 、 Testing advanced continuous integration 、 Test architecture development test framework 、 Performance testing 、 Safety test, etc. .

If my blog helps you 、 If you like my blog content , please “ give the thumbs-up ” “ Comment on ” “ Collection ” One button, three links ! Friends who like software testing , You can join our testing technology exchange group :779450660 There are various software testing resources and technical discussions )

原网站

版权声明
本文为[Peng Yuyan in the testing industry]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131307025735.html