Skip to content

Pass test result to finalizer for cleaning up debug data #288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
pytestbot opened this issue Apr 4, 2013 · 3 comments
Closed

Pass test result to finalizer for cleaning up debug data #288

pytestbot opened this issue Apr 4, 2013 · 3 comments
Labels
good first issue easy issue that is friendly to new contributor type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature

Comments

@pytestbot
Copy link
Contributor

Originally reported by: Torsten Landschoff (BitBucket: bluehorn, GitHub: bluehorn)


We are currently juggling with big files in tests which we currently store in tmpdir.
This kills our Jenkins slaves because py.test will keep temporary files for 3 runs of each test suite. We are no up at > 10GB of temporary files for some projects per test run.

Keeping the temporary files is indeed a nice feature which we often use during TDD to find out what we were missing. But the temporary files of a passed test are 99.99% worthless for us.

Therefore I'd like to have a tmpdir fixture that cleans up the temporary files if the test was successful or skipped. Or have some options to configure how py.test handles temporary files wrt. what is kept.

As an intermediate step I am currently moving some pytest.fixture functions to clean up the temporary files after use, but I end up removing the files even if the test failed.

What I am also just realizing: We moved the temporary files inside the Jenkins workspace of each project to keep track of disk usage and for easy access via the web interface. If only temporary files for failed tests are kept, then we could archive them as build artifacts so that we can even diagnose tests that failed in the past.

I was thinking about something in the line of

#!python

@pytest.fixture
def database(request):
    # Pull SQLite test database from somewhere
    dbfile = "..."
    def finalizer(testresult):
        if testresult == "PASS":
            os.unlink(dbfile)

    request.addfinalizer(finalizer, parameters=["testresult"])

Alternatively, pytest could use introspection to inject the testresult if the finalizer takes a testresult argument.


@pytestbot
Copy link
Contributor Author

Original comment by holger krekel (BitBucket: hpk42, GitHub: hpk42):


just quickly an example that works with today's pytest:

# content of conftest.py

import pytest

@pytest.fixture
def testresult():
    class TestResult:
        pass
    return TestResult()

@pytest.mark.tryfirst
def pytest_runtest_makereport(item, call, __multicall__):
    if call.when == "call":
        if "testresult" in item.fixturenames:
            rep = __multicall__.execute()
            item.funcargs["testresult"].rep = rep
            return rep

and the test file:

import pytest

@pytest.fixture
def database(request, tmpdir, testresult):
    p = tmpdir.ensure("somefile")
    def finalizer():
        if testresult.rep.passed:
            p.remove()
    request.addfinalizer(finalizer)
    return p

def test_pass(database):
    assert database.check()

def test_fail(database):
    assert 0

We could think about exposing something like "testresult" in standard pytest.

@pytestbot pytestbot added the type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature label Jun 15, 2015
pytestbot pushed a commit that referenced this issue Jun 15, 2015
…-punctuation-1430453576841 (pull request #288)

monkeypatch.chdir docstring punctuation
@pfctdayelise
Copy link
Contributor

I closed #230 as duplicate, maybe someone should make this as a plugin.

@pfctdayelise pfctdayelise added the good first issue easy issue that is friendly to new contributor label Jul 25, 2015
jboy pushed a commit to jboy/nim-pymod that referenced this issue Nov 28, 2015
…xtures.

We use this to:
* Clean up temporary files in the test directory, if the tests all passed.
* Preserve temporary files in the test directory (renamed with a "failed_" prefix) for later inspection, if any tests failed.

This required black magic, but Pytest really should make it easier for fixtures to access test results.  This is a known (reported) issue: pytest-dev/pytest#288
@RonnyPfannschmidt
Copy link
Member

closing this one as there is a supported way to do this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue easy issue that is friendly to new contributor type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature
Projects
None yet
Development

No branches or pull requests

3 participants