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 run a piece of code only one time before all tests run ? #3709

Closed
david-fliguer-ws opened this issue Jul 23, 2018 · 5 comments
Closed
Labels
type: question general question, might be closed after 2 weeks of inactivity

Comments

@david-fliguer-ws
Copy link

Hi,

I use pytest as my testing framework.

I would like some piece of code to run only one time before the tests start to run, I would like this also to work even if I run only some of the tests (For example using the -k parameter on the command line)

Where is the recommendation to write that code ? Use fixtures or traditional JUnit style ? Can someone explain more about this topic ?

Thanks a lot !!!

David

@david-fliguer-ws david-fliguer-ws changed the title How to run a piece of code only one time before all tests run How to run a piece of code only one time before all tests run ? Jul 23, 2018
@pytestbot
Copy link
Contributor

GitMate.io thinks possibly related issues are #1484 (running py.test), #3053 (How to set the staged of tests running?), #2544 (Run tests truly programmatically), #2713 (Database errors BEFORE running tests), and #471 (pytest keeps running deleted tests).

@pytestbot pytestbot added the platform: mac mac platform-specific problem label Jul 23, 2018
@RonnyPfannschmidt
Copy link
Member

hi @david-fliguer-ws ,

due to lack of a use-case I'm going to presume that an autouse session scoped fixture is sufficient
@pytest.fixture(scope="session", autouse=True)

there is more than just that to run code before tests, but in order to give more to the point advise we need to know what you are setting out to do instead of how you think it should be done

-- Ronny

@RonnyPfannschmidt RonnyPfannschmidt removed the platform: mac mac platform-specific problem label Jul 23, 2018
@nicoddemus nicoddemus added the type: question general question, might be closed after 2 weeks of inactivity label Jul 23, 2018
@asottile
Copy link
Member

@david-fliguer-ws hope the above comment helped you resolve this! If not please comment here and we can reopen / provide more direction :)

@martinvol
Copy link

As a walk-around, code inside __init__.py file in your tests folder will execute before all tests.

@RonnyPfannschmidt
Copy link
Member

It will also execute if no test runs, which may be a problem

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

6 participants