Hello! I want to test my big program. With structure like this:
/root
app
-packet1
--directory_for_remove
--module1.py
--mudule2.py
-packet2
--module1.py
tests
-packet1
--test_module1.py
--test_module2.py
-packet2
--test_module1.py
-conftest.py
But to test it I need to create data files with my account's data. As you understand I can't to delete it after first test with its creation. It means, that I need to delete it only after working of all tests.
First problem. I tried to create function teardown() in conftest and it is not work. Is it a best idea to add addfinalizer in last test wich musth be passed?
Second problem. As I understand after exceptions teardown method is not work?
GitMate.io thinks the contributors most likely able to help are @nicoddemus, and @RonnyPfannschmidt.
And third problem. In test_module1.py I have script like this:
def teardown():
print(f'Now we are at {getcwd()}')
rmtree(join('..', '..', 'packet1', 'directory_for_remove'))
And when I run tests from 'tests' directory, like this 'pytest -q tests', I have error output and print like this: 'Now we are at /root/app/tests'. And when I run pytest from script like 'pytest -q test_module1.py', it is work well.
Is is ok? How can I do to avoid incorrect relative paths?
def pytest_unconfigure(config): -- put this in your conftest.py.
@robert-cody Thank you very much! It is cool. But what about my third problem?
By the way I have an open file functions in test_module1.py which open file located in 'directory to remove'. How can I resolve problem with relative paths?
Are any better ways than
open(join(dirname(__file__), '..', '..', 'directory_for_remove', 'file_name'))
in test_module1.py?
@multisteam I suggest to take a look at fixtures, they are designed specifically to solve this kind of problem. 馃槈
In your case I think you should have a session-scoped fixture which creates the data files into a temporary directory (using tmpdir_factory):
# in conftest.py
@pytest.fixture(scope='session')
def data_files_dir(tmpdir_factory):
datadir = tmpdir_factory.mktemp('data')
create_data_files(str(datadir))
return datadir
When the last test in your session terminates (regardless if it failed or passed), the directory will be removed automatically because of the tmpdir_factory fixture.
Now tests can use that fixture to access the data directory:
def test_some_data(data_files_dir):
contents = data_files_dir.join('somefile.cvs').read()
...
You may also take a look at pytest-datadir.
@nicoddemus I understand this. But I need to create this file in this directory because the main part of other tests will depends from this file. May be I can to write monkeypatch to all tests with paths to this file, but would it be better way?
But I need to create this file in this directory because the main part of other tests will depends from this file
The ideal approach would be to change the tests to be able to use the fixture, but if that's not possible (I'm guessing also your application code has this path hardcoded somewhere) then monkeypatching would be the recommended solution. You can use an autouse-fixture to monkeypatch all tests automatically.
Closing this for now, feel free to reopen with further questions though.
There's my solution of the problem depending on @nicoddemus solution (put this in conftest.py):
@pytest.fixture(scope='session')
def clear_files_teardown():
yield None
os.system("rm -rf logs json")
Most helpful comment
def pytest_unconfigure(config): -- put this in your conftest.py.