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

How to set the staged of tests running? #3053

Closed
mihalt opened this issue Dec 20, 2017 · 7 comments
Closed

How to set the staged of tests running? #3053

mihalt opened this issue Dec 20, 2017 · 7 comments
Labels
type: question general question, might be closed after 2 weeks of inactivity

Comments

@mihalt
Copy link

mihalt commented Dec 20, 2017

Hello! My project has structure like this:

app
-dir1
--module1.py
--mudule2.py

-dir2
--module1.py

-tests
--dir1
---test_module1.py
---test_module2.py
---conftest.py
--dir2
---test_module1.py
---conftest.py
--conftest.py

In test_module1.py I create and test creating important data-files from wich depends all other programm tests. On tests.conftest.py all this data is cleared.

But I am not understand, why when I execute 'pytest tests' run test_module2.py at first. And of course it is fail because data from test_module1.py is not exist on its time. And one more fact, that after that test_module1.py don't run at all.

Say me please, how can I set the steps of running my tests?

@pytestbot
Copy link
Contributor

GitMate.io thinks the contributor most likely able to help you is @robert-cody.

@brianmaissy
Copy link
Contributor

The fact that one test is implicitly dependent on another is probably not a good idea.

You would be better of creating the data files in a fixture, for example, and then all the tests which require the files (including the test that validates them) can request the fixture.

But if you really want to control the order of the tests, you can do that using the pytest_collection_modifyitems hook: https://docs.pytest.org/en/latest/writing_plugins.html#_pytest.hookspec.pytest_collection_modifyitems

@mihalt
Copy link
Author

mihalt commented Jan 2, 2018

@brianmaissy It seems, that controlling the order of the test would be better in my case, bacause I want to test where file is saved. When it is used by script from other working directories the ralatives paths can be other to save files and different scripts which used this files can be not work.

Can you give me some example of code that change the order of the tests? And links in docs, that you have used to write this? I don't understand how it must be used in link upper.

@brianmaissy
Copy link
Contributor

Given the following tests:

def test_requires_file():
    assert open('temp', 'rb').read() == 'hello'


def test_creates_file():
    open('temp', 'wb').write('hello')

This hook implementation could move the test which creates the file to the beginning of the list:

def pytest_collection_modifyitems(session, config, items):
    file_creator_test = [test_function for test_function in items if test_function.name == 'test_creates_file'][0]
    items.remove(file_creator_test)
    items.insert(0, file_creator_test)

Or even better, take a look at the pytest-ordering plugin, which does things like this for you:
http://pytest-ordering.readthedocs.io/en/develop/

@nicoddemus
Copy link
Member

It seems, that controlling the order of the test would be better in my case, bacause I want to test where file is saved.

Note you can do all that with a fixture (fixtures can also use assert for tests). This is the really recommended way to go about this, but otherwise you can go with @brianmaissy's advice.

@nicoddemus nicoddemus added the type: question general question, might be closed after 2 weeks of inactivity label Jan 9, 2018
@brianmaissy
Copy link
Contributor

This can probably be closed

@nicoddemus
Copy link
Member

Thanks @brianmaissy for the ping! 👍

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

4 participants