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
27
votes
4 answers

What's the benefit of a fixture with function scope and no teardown code?

What's advantage of a (default) function-scope fixture without teardown code? Why not just call the function at the beginning of the test? For example, what's the benefit of writing: @pytest.fixture def smtp(): return…
max
  • 49,282
  • 56
  • 208
  • 355
27
votes
2 answers

Access autouse fixture without having to add it to the method argument

I have a session scoped fixture in conftest.py @pytest.fixture(scope="session",autouse=True) def log(request): testlog = LogUtil(testconf.LOG_NAME).get() return testlog This is loaded and works as expected when a test method is defined in…
tjmn
  • 469
  • 1
  • 6
  • 11
27
votes
1 answer

Pytest not recognizing added options

I have a project directory looks like following Projects/ ....this_project/ ........this_project/ ............__init__.py ............code.py ............tests/ ................conftest.py ................test_1.py ................test_2.py and I…
Hello lad
  • 17,344
  • 46
  • 127
  • 200
26
votes
3 answers

Why is caplog.text empty, even though the function I'm testing is logging?

I'm trying to use pytest to test if my function is logging the expected text, such as addressed this question (the pyunit equivalent would be assertLogs). Following the pytest logging documentation, I am passing the caplog fixture to the tester. …
gerrit
  • 24,025
  • 17
  • 97
  • 170
26
votes
5 answers

How to start a Uvicorn + FastAPI in background when testing with PyTest

I have an REST-API app written with Uvicorn+FastAPI Which I want to test using PyTest. I want to start the server in a fixture when I start the tests, so when the test complete, the fixture will kill the app. FastAPI Testing shows how to test the…
RaamEE
  • 3,017
  • 4
  • 33
  • 53
26
votes
1 answer

Pytest skips test saying "asyncio not installed"

When testing the following code @pytest.mark.asynico async def test_handle_DATA(mocker): handle_mock = mocker.MagicMock() envelope_mock = mocker.MagicMock(mail_from="Test@From", rcpt_tos=["Test@To"], content=b"TestContent") result =…
Daniel Butler
  • 3,239
  • 2
  • 24
  • 37
26
votes
2 answers

How to check if a function was called in a unit test using pytest-mock?

I have a unit test where I want to check if a function was called. How do I do this withpytest and pytest-mock libraries? For example, here is a unit test test_hello.py. In this test I call the function my_function and want to verify that it called…
Evgenii
  • 36,389
  • 27
  • 134
  • 170
26
votes
3 answers

Audit url open for permitted schemes. Allowing use of "file:" or custom schemes is often unexpected

I am using this statement in Python: jsonreq = json.dumps({'jsonrpc': '2.0', 'id': 'qwer', 'method': 'aria2.pauseAll'}) jsonreq = jsonreq.encode('ascii') c = urllib.request.urlopen('http://localhost:6800/jsonrpc', jsonreq) I am getting this…
Kapil Sharma
  • 263
  • 3
  • 4
26
votes
2 answers

How to assert ValueError in pytest

I am writing pytest unit tests for the following function from datetime import datetime def validate_timestamp(timestamp): """Confirm that the passed in string is in the proper format %Y%m%d_%H""" try: ts =…
sakurashinken
  • 3,940
  • 8
  • 34
  • 67
26
votes
2 answers

How do I import the pytest monkeypatch plugin?

I want to use the pytest monkeypatch plugin, but I can't figure out how to import it. I've tried: import monkeypath import pytest.monkeypatch from pytest import monkeypatch
John Bachir
  • 22,495
  • 29
  • 154
  • 227
26
votes
3 answers

Pytest report summary to display error information

I am relatively new to pytest hooks and plugins and I am unable to figure out how to get my pytest code to give me test execution summary with reason of failure. Consider the code: class Foo: def __init__(self, val): self.val = val …
Numa
  • 261
  • 1
  • 3
  • 3
26
votes
4 answers

pytest: Reusable tests for different implementations of the same interface

Imagine I have implemented a utility (maybe a class) called Bar in a module foo, and have written the following tests for it. test_foo.py: from foo import Bar as Implementation from pytest import mark @mark.parametrize(,
jacg
  • 2,040
  • 1
  • 14
  • 27
26
votes
3 answers

Why cant unittest.TestCases see my py.test fixtures?

I'm trying to use py.test's fixtures with my unit tests, in conjunction with unittest. I've put several fixtures in a conftest.py file at the top level of the project (as described here), decorated them with @pytest.fixture, and put their names as…
Will
  • 4,241
  • 4
  • 39
  • 48
25
votes
4 answers

How to share parametrized arguments across multiple test functions?

I have many test functions in the same file that share a common set of parametrized arguments, as shown in the code snippets below. When the number of test functions increases (>100), repeating those parameters for each test function doesn't seem to…
cuhor
  • 265
  • 1
  • 3
  • 7
25
votes
2 answers

assertTrue() in pytest to assert empty lists

Is there a way to use assertTrue() or assertFalse() like a function in pytest for python unittests? I have a function which returns a list of elements. If the list is empty the test needs to fail through assertion. Is there anything like…
cool77
  • 1,086
  • 4
  • 15
  • 31