You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The text was updated successfully, but these errors were encountered:
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).
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:
But delete_data_directory runs during collection, before test time. So instead, I'm looking at using a hack like this:
What's the right way to accomplish something like this?
edit: The 'autouse' argument makes this much cleaner; I'll just go with this.
The text was updated successfully, but these errors were encountered: