简体   繁体   中英

can we define pytest hooks outside conftest.py?

i did some research in the sites below but i haven't still figured out if we can define pytest hooks (eg pytest_runtest_makereport) other than in the conftest.py file. basically, what i am trying to figure out is if i avoid duplicating conftest.py files by defining a hook inside a base class, for example, so other projects (or classes) consuming this base class can inherit these hooks.

thanks a lot.

references i've used:

The recommended way would be to move this hooks into an appropriate plugin, but you can force a module to be interpreted as one by declaring a pytest_plugins variable in a conftest file in your project:

pytest_plugins = ['myproject.plugin'] # myproject.plugin contains hooks

Or you can use the -p flag to py.test :

py.test -p myproject.module

Note that the last option can be configured in your pytest.ini as well:

[pytest]
addopts = -p myproject.module

There is a easy way to achieve that, just put all pytest hooks in a python file, and import it to the conftest.py file in your test pillar folder.

For example: in conftest.py of your test pillar

from custom_hooks import *

Then you can reuse all hooks.

And if you want to overwrite one, just define it again in conftest.py.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM