Questions tagged [pytest]

For questions about the pytest Python testing tool. Please also add the [python] tag for questions tagged with [pytest].

pytest is a mature, fully featured testing tool that simplifies the testing experience, which:

  • Provides no-boilerplate testing.
  • Supports functional testing and complex test setups.
  • Integrates many common testing methods.
  • Offers extensive plugin and customization system.

Be sure to check the Getting Started docs to see how easy it is to get running with pytest. The docs are comprehensive and development is open to contributions.

9822 questions
88
votes
7 answers

Getting error ImportMismatchError while running py.test

When I am running tests locally its working fine, but after creating the docker and running inside the container I am getting below error. /usr/local/lib/python3.5/site-packages/_pytest/config.py:325: in _getconftestmodules return…
A J
  • 3,684
  • 2
  • 19
  • 24
88
votes
1 answer

nose vs pytest - what are the (subjective) differences that should make me pick either?

I've started working on a rather big (multithreaded) Python project, with loads of (unit)tests. The most important problem there is that running the application requires a preset environment, which is implemented by a context manager. So far we made…
Jakob van Bethlehem
  • 1,066
  • 1
  • 7
  • 10
87
votes
3 answers

Using pytest with a src layer

pytest recommends including an additional directory to separate the source code within a project: my_package ├── src # <-- no __init__.py on this layer │   └── my_package │   ├── __init__.py │   └── util_module │   ├──…
Arne
  • 17,706
  • 5
  • 83
  • 99
87
votes
4 answers

How do I use pytest with virtualenv?

I installed pytest into a virtual environment (using virtualenv) and am running it from that virtual environment, but it is not using the packages that I installed in that virtual environment. Instead, it is using the main system packages. (Using…
Henry Grantham
  • 1,025
  • 2
  • 8
  • 11
86
votes
7 answers

How to run unittest discover from "python setup.py test"?

I'm trying to figure out how to get python setup.py test to run the equivalent of python -m unittest discover. I don't want to use a run_tests.py script and I don't want to use any external test tools (like nose or py.test). It's OK if the…
cdwilson
  • 4,310
  • 4
  • 26
  • 32
83
votes
3 answers

How to run pytest tests in parallel?

I want to run all my pytest tests in parallel instead of sequentially. my current setup looks like: class Test1(OtherClass): @pytest.mark.parametrize("activity_name", ["activity1", "activity2"]) @pytest.mark.flaky(reruns=1) def…
Noy Mizrahi
  • 831
  • 1
  • 6
  • 4
83
votes
7 answers

How can I repeat each test multiple times in a py.test run?

I want to run each selected py.test item an arbitrary number of times, sequentially. I don't see any standard py.test mechanism for doing this. I attempted to do this in the pytest_collection_modifyitems() hook. I modified the list of items passed…
Martin Del Vecchio
  • 3,558
  • 2
  • 27
  • 36
82
votes
10 answers

How to test a function with input call?

I have a console program written in Python. It asks the user questions using the command: some_input = input('Answer the question:', ...) How would I test a function containing a call to input using pytest? I wouldn't want to force a tester to…
Zelphir Kaltstahl
  • 5,722
  • 10
  • 57
  • 86
81
votes
7 answers

TypeError: attrib() got an unexpected keyword argument 'convert'

This error occurred during automated testing of a python project on the CI server using pytest. I'm using pytest==4.0.2. This error only just started to occur, previous pipelines seem to work fine. The full error: File…
nitred
  • 5,309
  • 3
  • 25
  • 29
80
votes
4 answers

Test discovery failure when tests in different directories are called the same

Using py.test, two tests called the same in different directory causes py.test to fail. Why is that? How can I change this without renaming all the tests? To duplicate do: ; cd /var/tmp/my_test_module ; mkdir -p ook/test ; mkdir -p…
Sardathrion - against SE abuse
  • 17,269
  • 27
  • 101
  • 156
75
votes
5 answers

Pytest monkeypatch isn't working on imported function

Suppose there are two packages in a project: some_package and another_package. # some_package/foo.py: def bar(): print('hello') # another_package/function.py from some_package.foo import bar def call_bar(): # ... code ... bar() #…
Glueon
  • 3,727
  • 4
  • 23
  • 32
75
votes
4 answers

Pytest where to store expected data

Testing function I need to pass parameters and see the output matches the expected output. It is easy when function's response is just a small array or a one-line string which can be defined inside the test function, but suppose function I test…
Glueon
  • 3,727
  • 4
  • 23
  • 32
74
votes
2 answers

"py.test" vs "pytest" command

The py.test command is failing in my case, whereas pytest is running totally fine. I use the pytest-flask plugin: platform linux -- Python 3.5.2, pytest-3.0.2, py-1.4.31, pluggy-0.3.1 rootdir: /home/sebastian/develop/py/flask-rest-template, inifile:…
swiesend
  • 1,051
  • 1
  • 13
  • 23
73
votes
3 answers

How to keep Unit tests and Integrations tests separate in pytest

According to Wikipedia and various articles it is best practice to divide tests into Unit tests (run first) and Integration tests (run second), where Unit tests are typically very fast and should be run with every build in a CI environment, however…
Tom Malkin
  • 2,134
  • 2
  • 19
  • 35
71
votes
15 answers

Py.test command not found, but library is installed

There are already two posts on stack overflow on this topic; however, none of them have resolved or addressed my specific situation. I have installed pytest via pip install pytest. I am able to import the library in Python as well. The problem is…
E. Otero
  • 901
  • 1
  • 8
  • 13