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
38
votes
3 answers

pytest running with another version of python

I've installed pyenv and have different versions of python installed with it: $ pyenv versions system 2.7.1 3.2.5 3.5.0 3.5.1 * 3.5.2 I use the following command to switch to python 3.5.2: pyenv shell 3.5.2 And when I check the python…
B Faley
  • 17,120
  • 43
  • 133
  • 223
38
votes
1 answer

How can I use pytest.raises with multiple exceptions?

I'm testing code where one of two exceptions can be raised: MachineError or NotImplementedError. I would like to use pytest.raises to make sure that at least one of them is raised when I run my test code, but it only seems to accept one exception…
fenceop
  • 1,439
  • 3
  • 18
  • 29
38
votes
4 answers

Py.test: parametrize test cases from classes

I'm currently following this py.test example and it works out when I do not use classes, however when I introduce test cases into classes I am failing. The smallest case I managed to write is the following: import unittest import pytest class…
pafede2
  • 1,626
  • 4
  • 23
  • 40
38
votes
1 answer

How to show warnings in py.test

I just ran py.test on my code and got the following output: ================== 6 passed, 2 pytest-warnings in 40.79 seconds ======================= However, I cannot see what py.test would like to warn me about. How can I turn on warning output to…
Sebastian Wozny
  • 16,943
  • 7
  • 52
  • 69
38
votes
3 answers

py.test does not find tests under a class

I am trying to create test classes that aren't unittest based. This method under this class class ClassUnderTestTests: def test_something(self): cannot be detected and run when you call py.test from the command line or when you run this test…
Michael
  • 913
  • 3
  • 10
  • 18
37
votes
4 answers

When to use pytest fixtures?

I'm new to testing and I've stumbled across pytest fixtures, but I'm not entirely sure when to use them and why they're useful. For example, see the below code: import pytest @pytest.fixture def input_value(): input = 39 return input def…
the man
  • 1,131
  • 1
  • 8
  • 19
37
votes
1 answer

pytest fixture of fixture, not found

Based on this stackoverflow: pytest fixture of fixtures I have the following code in the same file: @pytest.fixture def form_data(): return { ... } @pytest.fixture def example_event(form_data): return {... 'data': form_data, ... } But when…
James Lin
  • 25,028
  • 36
  • 133
  • 233
36
votes
2 answers

py.test patch on fixture

I use the following to mock constant values for a test with py.test: @patch('ConstantsModule.ConstantsClass.DELAY_TIME', 10) def test_PowerUp(): ... thing = Thing.Thing() assert thing.a == 1 This mocks DELAY_TIME as used in both the…
sg_man
  • 763
  • 1
  • 6
  • 14
36
votes
3 answers

How to use pytest to assert NO Warning is raised

I want to ensure that no warning at all is raised in one assertion. Could not find any explicit answer in pytest documentation about warnings. (dead link, the 3.2.* doc is not available anymore). I've tried this, thinking maybe None would mean…
zezollo
  • 4,606
  • 5
  • 28
  • 59
36
votes
1 answer

Django test VS pytest

I am new to django unittest and pytest. However, I started to feel that pytest test case is more compact and clearer. Here is my test cases: class OrderEndpointTest(TestCase): def setUp(self): user =…
joe
  • 8,383
  • 13
  • 61
  • 109
36
votes
2 answers

Override a pytest parameterized functions name

My parameters determine the name of my parameterized pytest. I will be using a some randomized params for these tests. In order for my reporting names in junit to not get messed up, I'd like to create a static name for each parameterized test. Is it…
SomeGuyOnAComputer
  • 5,414
  • 6
  • 40
  • 72
36
votes
2 answers

Can PyCharm drop into debug when py.test tests fail

When running tests with py.test there is a --pdb option to enter pdb on failure. Is there a similar way to enter the debugger when running the same test from within PyCharm?
Johan Lübcke
  • 19,666
  • 8
  • 38
  • 35
36
votes
2 answers

writing a pytest function to check outputting to a file in python?

I asked this question about how to write a pytest to check output in stdout and got a solution. Now I need to write a test case, to check if the contents are written to the file and that the contents are written as expected eg: def writetoafile(): …
brain storm
  • 30,124
  • 69
  • 225
  • 393
36
votes
9 answers

How to skip the rest of tests in the class if one has failed?

I'm creating the test cases for web-tests using Jenkins, Python, Selenium2(webdriver) and Py.test frameworks. So far I'm organizing my tests in the following structure: each Class is the Test Case and each test_ method is a Test Step. This…
Alex Okrushko
  • 7,212
  • 6
  • 44
  • 63
35
votes
2 answers

VS Code / Python / Debugging pytest Test with the Debugger

How can I get VS Code to put me into the debugger at the point of failure when running tests with pytest? Pytest catches all errors and asserts, and VS code invokes the debugger only on uncaught errors (I can change this to raised exception, but…
mgf
  • 401
  • 1
  • 4
  • 7