2

I have a Django project with multiple apps. Each app has a set of unittests. I'm using pytest as my test runner. We have gotten to a point that we want to start writing integration tests. I was wondering if there is any way to keep the naming convention and thus the auto discovery of pytest but still be able (via flag maybe?) to run the different test types. The most intuitive solution that comes to mind is some sort of decorator on test methods or even TestCase classes (something like Category in JUnit).
something like:

@testtype('unittest')
def test_my_test(self):
    # do some testing

@testtype('integration')
def test_my_integration_test(self):
    # do some integration testing

and then i could run the test like:

py.test --type=integration
py.test --type=unittest

Is there such a thing?
If not, the only other solution i can think about is to add a django command and "manually" build a testsuite and run it with pytest... I would prefer not to use this option. Is there any other solution that can help me?
Thanks

2 Answers 2

10

You can mark test functions.

import pytest

@pytest.mark.unittest
def test_my_test(self):
    # do some testing

@pytest.mark.integration
def test_my_integration_test(self):
    # do some integration testing

These custom markers must be registered in your pytest.ini file.

Then use the -m flag to run the marked tests

py.test -v -m unittest

Another option would be to split your tests into unittest and integration directories. Then you can run tests in a specific directory with:

py.test -v unittest

or

py.test -v integration
Sign up to request clarification or add additional context in comments.

Comments

0

Another way to do this (without any config or code)

pytest -o "python_functions=*_integration_test"

You can also do this in module/class level, e.g.,

python_files = integration_test_*.py
python_classes = IntegrationTest

Ref: https://docs.pytest.org/en/latest/example/pythoncollection.html#changing-naming-conventions

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.