Initial and final check when running pytest test suite
<p>I have productive code which creates config files in my <code>$HOME</code> folder and to run my tests in an isolated environment I patch <code>$HOME</code> in <code>conftest.py</code>. Still I'm not sure if this works in general and maybe unthoughtful written test functions might break out.</p>

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

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

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

<hr>

<p>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. </p>

<p>Have you considered writing a session-scoped pytest fixture? Something like:</p>

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

    def final_check(request):
        assert final_condition

    request.addfinalizer(final_check)

    return request
</code></pre>

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

python py.test