Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use conftest.py Files

Shulou Source: shulou.com Published: 2022-06-03 07:15:16 09月15日 Update

This paper takes the conftest.py file of Python as an example to analyze the usage of conftest.py file. Read the complete article, I believe that you have a certain understanding of the use of conftest.py files.

1.conftest.py and test.py are placed in the same directory to achieve global uniqueness at the session level.

two。 Examples of initialization operation

In the conftest.py file, define a class, class variables are instantiated in the initialization method, and the class can be used in the py file. Variable to call the object, ensuring that it is initialized only once

# # conftest.py

Class Test1: dr = None # type: classss = None # type:Ssclass Ss: def _ init__ (self): self.__cs = Test1.dr def pr1 (self): print ("method 1" + self.__cs) def pr2 (self): print ("method 2" + self._ _ cs) def close (self): print ("exit") print ("end") @ pytest.fixture (scope='session') Autouse=True) def chishihua (request): Test1.dr = "ces" Test1.classss = Ss () print ("all") def fin (): Test1.classss.close () # differences in autouse parameters for close operation request.addfinalizer (fin)

Do not use autouse=True, you need to call the initialization method in other test methods, and if you use autouse=True, you do not need to call the addfinalizer method to finish the work after session. Do you have any further understanding of the use of conftest.py files? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel. Thank you for reading.

Tags: Method file usage content variable more global parameter instance object difference skill directory level industry information information channel channel enter one guarantee Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info MySQL Shulou Technology Shulou Information macOS