[TIP] How to use same pytest hook at module and function scopes?

arun kali raja arunsep886 at gmail.com
Wed Feb 8 06:19:56 PST 2017


down votefavorite
<http://stackoverflow.com/questions/42113519/how-to-use-same-pytest-hook-at-module-and-function-level#>

I am new to pytest and trying to use pytest for my testing purpose.

The TestCase layout which i have is

common_configuration

TC_1
TC_2
TC_3

Now i have a requirement to take backup of configuration before each TC and
also before common_configuration is done and restore it after all the TCs
are done..

i am planning to declare common_configuration itself as a module level
fixture..

For the configuration backup and reversal part, i am trying to implement
that also as fixtures(and its finaliser to reverse configuration)..

So the flow i am trying to achieve is

configurationBackup
common_Configuration
configurationBackup
TC_1
configurationBackup.finalizer
configurationBackup
TC_2
configurationBackup.finalizer
configurationBackup
TC_3
configurationBackup.finalizer
configurationBackup.finalizer(which is done before common_Configuration)

I tried couple of things.

   1.

   declaring configurationBackup in conftest.py as a function level
   fixture)as an autouse fixture). But the problem here is its executed before
   common_configuration fixture for TC_1
   2.

   tried using standar pytest hooks. There is a pytest hook
   pytest_runtest_call.. i was thinking if i can decorate this hook function
   with my configurationBackup fixture like this:

      @pytest.mark.usefixtures("configurationBackup")
      def pytest_runtest_call(item):


But this also doesnt seem to work... my configurationBackup fixture is not
called at all..

Any other ways to solve this usecase?
                I understand that a session level fixture can be called
from module level..(so from lower scope we can call higher scope
fixture)..I tried declaring the configuration Backup as session level
fixture and calling it from module scope/function scope.. but the fixture
was called once in session scope and was not called again in module scope..
can the same fixture be called at two levels?
-- 
அன்புடன்,
பா. அருண் காளி ராசா
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20170208/ce7a1d51/attachment.html>


More information about the testing-in-python mailing list