Skip to content
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

(question) Cleaning up from parameter generation #3601

Closed
beaumitch opened this issue Jun 19, 2018 · 1 comment
Closed

(question) Cleaning up from parameter generation #3601

beaumitch opened this issue Jun 19, 2018 · 1 comment
Labels
type: question general question, might be closed after 2 weeks of inactivity

Comments

@beaumitch
Copy link

beaumitch commented Jun 19, 2018

Hi, I'm wondering what the best practices are for cleaning up after a function that generates parameters for an @parametrize .
Here's a minimal example of what I'd like to accomplish:

import pytest

def get_test_files():
    data_directory = generate_data_directory()
    for filename in data_directory:
        yield filename
    delete_data_directory()

@pytest.mark.parametrize("filename", get_test_files())
def test_file(filename):
    with open('./data/' + filename, 'rb') as test_file:
        assert my_module.process_file(test_file) == expected_result

But delete_data_directory runs during collection, before test time. So instead, I'm looking at using a hack like this:

import pytest

def get_test_files():
    global data_directory
    data_directory = generate_data_directory()
    for filename in data_directory:
        yield filename

@pytest.fixture(scope="session")
def teardown():
    yield None
    delete_data_directory(data_directory)

@pytest.mark.parametrize("filename", get_test_files())
def test_file(filename, teardown):
    with open('./data/' + filename, 'rb') as test_file:
        assert my_module.process_file(test_file) == expected_result

What's the right way to accomplish something like this?

edit: The 'autouse' argument makes this much cleaner; I'll just go with this.

import pytest

def get_test_files():
    global data_directory
    data_directory = generate_data_directory()
    for filename in data_directory:
        yield filename

@pytest.fixture(scope="session", autouse=True)
def teardown():
    yield
    delete_data_directory(data_directory)

@pytest.mark.parametrize("filename", get_test_files())
def test_file(filename):
    with open('./data/' + filename, 'rb') as test_file:
        assert my_module.process_file(test_file) == expected_result
@pytestbot pytestbot added the type: question general question, might be closed after 2 weeks of inactivity label Jun 19, 2018
@pytestbot
Copy link
Contributor

GitMate.io thinks possibly related issues are #1120 (Cleaning up tmpdir's), #2272 (Question: Can callable as argument to ids parameter of parametrize of operate on a tuple?), #543 (Should tmpdir clean up after itself?), #1748 (deprecate unpacking marks in parameter values and provide a clean alternative), and #1843 (Parameter corruption for parameterized tests).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question general question, might be closed after 2 weeks of inactivity
Projects
None yet
Development

No branches or pull requests

2 participants