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
35
votes
2 answers

What is the correct order for actual and expected in pytest?

This question gives the order assertEqual(expected, actual), albeit for the unittest package. But Pycharm, with pytest, prints out "Expected:..." and "Actual..." based on the order actual==expected. This is confusing. What is the correct ordering…
Joshua Fox
  • 18,704
  • 23
  • 87
  • 147
35
votes
2 answers

Mocking a module import in pytest

I am writing a pytest plugin that should test software that's designed to work inside a set of specific environments. The software I'm writing is run inside a bigger framework, which makes certain Python modules available only when running my Python…
NirIzr
  • 3,131
  • 2
  • 30
  • 49
34
votes
3 answers

Proper way to return mocked object using pytest.fixture

I'm trying to setup the target under test in @pytest.fixture and use it in all my tests in the module. I'm able to patch the test correctly, but after I add the @pytest.fixture to return the mock object and invoke the mocked object in other unit…
Bryan Cheng
  • 403
  • 1
  • 5
  • 11
34
votes
1 answer

using mocker to patch with pytest

I installed pytest-mock and using a mocker I am trying to function as patch does, but I get "Type Error: Need a valid target to patch. You supplied 'return a + b'" # test_capitalize.py import time def sum(a, b): time.sleep(10) return a +…
Kevin
  • 667
  • 2
  • 7
  • 18
34
votes
6 answers

How do I get PyCharm to show entire error diffs from pytest?

I am using Pycharm to run my pytest unit tests. I am testing a REST API, so I often have to validate blocks of JSON. When a test fails, I'll see something like this: FAILED test_document_api.py:0 (test_create_documents) {'items': [{'i...ages': 1,…
W.P. McNeill
  • 16,336
  • 12
  • 75
  • 111
34
votes
6 answers

Pytest use same fixture twice in one function

For my web server, I have a login fixture that create a user and returns the headers needed to send requests. For a certain test, I need two users. How can I use the same fixture twice in one function? from test.fixtures import login class…
danijar
  • 32,406
  • 45
  • 166
  • 297
34
votes
3 answers

PyCharm noinspection for whole file?

Is it possible to disable an inspection for the whole file in PyCharm? The reason this is needed is when dealing with py.test. It uses fixtures which appear to shadow function parameters, and at the same time cause unresolved references. e.g.: from…
gak
  • 32,061
  • 28
  • 119
  • 154
33
votes
1 answer

pytest ScopeMismatch error: how to use fixtures properly

For the following piece of code: @pytest.fixture(scope="module") def dummy_article(request, db): return mixer.blend("core.article", title="this one price", internal_id=3000) def test_article_str_method(dummy_article): assert ( …
zerohedge
  • 3,185
  • 4
  • 28
  • 63
33
votes
2 answers

pytest assert introspection in helper function

pytest does wonderful assert introspection so it is easy to find differences in strings especially if the difference is in white space. Now I use a slightly complicated test helper that I reuse in many testcases. The helper has its own module, too…
moin moin
  • 2,263
  • 5
  • 32
  • 51
33
votes
4 answers

coverage.py does not cover script if py.test executes it from another directory

I got a python script which takes command line arguments, working with some files. I'm writing succeeding tests with py.test putting this script through its paces, executing it with subprocess.call. Now I want to analyze code coverage with…
Christoph
  • 5,480
  • 6
  • 36
  • 61
32
votes
3 answers

How to get PyTest working in Visual Studio

I want to integrate PyTest with Visual Studio so my tests show up in the Test Explorer. So far I have not found any way to do this while some old posts here suggest that people have done it before. As far as I can tell, as this article suggests,…
Barka
  • 8,764
  • 15
  • 64
  • 91
32
votes
6 answers

shortcut to run current Python unit test in VSCode

The question is regarding Visual Studio Code (VSCode from here) and python VSCode extension that finds and runs py.test tests. Is it possible to assign some shortcut to run current (under cursor) test method and/or test class? I really like the…
mislavcimpersak
  • 2,880
  • 1
  • 27
  • 30
32
votes
4 answers

pytest fixtures in a separate directory

I'm looking to create a pytest structure where I can separate the fixtures from the tests completely. The reason for this separation is that I want to include the fixtures directory as an external item in subversion and share it between multiple…
Jakob Pogulis
  • 1,150
  • 2
  • 9
  • 19
31
votes
3 answers

running a package pytest with poetry

I am new to poetry and want to get it set-up with pytest. I have a package mylib in the following set-up ├── dist │   ├── mylib-0.0.1-py3-none-any.whl │   └── mylib-0.0.1.tar.gz ├── poetry.lock ├── mylib │   ├── functions.py │   ├── __init__.py │  …
Mike
  • 3,775
  • 8
  • 39
  • 79
31
votes
4 answers

pytest and Failed: Database access not allowed, use the "django_db" mark, or the "db" or "transactional_db" fixtures to enable it

During invoking pytest from the shell I get the following output, because my test is stored in apps.business.metrics.tools.tests.py, and during import of the module apps/business/metrics/widgets/employees/utilization.py makes a live call to SQL…
DmitrySemenov
  • 9,204
  • 15
  • 76
  • 121