8

I have productive code which creates config files in my $HOME folder and to run my tests in an isolated environment I patch $HOME in conftest.py. Still I'm not sure if this works in general and maybe unthoughtful written test functions might break out.

To ensure the validity of my test suite I'd like to run a preliminary check of the respective files in $HOME and I'd like to run a final check after running the test suite.

How can I achieve this with the "official" means of pytest ? I have a dirty hack which works but messes up reporting.

My test suite is correct now and this question is out of curiosity because I'd like to learn more about pytest.


Addition: Same question, but different use case: I'd like to check if a 3rd-party plugin fullfills a version requierement. If this is not the case I'd like to show a message and stop py.test.

rocksportrocker
  • 7,251
  • 2
  • 31
  • 48
  • have you tried setting the `$HOME` environment variable when running your test? The instructions on how to do this change on different operating systems – Daniel Kravetz Malabud Aug 31 '16 at 20:14
  • Yes, this is what I do already. Still I had a wrong test function which circumvented this and my intention is to implement an addtional "meta" test which ensures that my test suite itself is correct. My question is out of cuorisity to learn more about the features of `pytest`. – rocksportrocker Sep 01 '16 at 05:57

1 Answers1

1

Have you considered writing a session-scoped pytest fixture? Something like:

@pytest.fixture(scope="session")
def global_check(request):
    assert initial_condition

    def final_check(request):
        assert final_condition

    request.addfinalizer(final_check)

    return request

If all of your other fixtures inherit from global_check, then initial_condition will be asserted at the beginning of all of your test runs and final_condition will be asserted at the end of your test runs.

Max Gasner
  • 1,191
  • 1
  • 12
  • 18
  • 1
    I tried something like this but then got a report with a long list of failed test functions instead of one single message + stop in the beginning. I extended this approach with `autotest = True` so I did not have to inherit from this fixture. – rocksportrocker Sep 16 '16 at 08:13
  • Fair enough. Here's an interesting approach: http://stackoverflow.com/a/9122053/324449 – Max Gasner Oct 18 '16 at 17:16