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

How to print output when using pytest with xdist

I'm using py.test to run tests. I'm using it with pytest-xdist to run the tests in parallel. I want to see the output of print statements in my tests. I have: Ubuntu 15.10, Python 2.7.10, pytest-2.9.1, pluggy-0.3.1. Here's my test file: def…
Steve Saporta
  • 4,581
  • 3
  • 30
  • 32
25
votes
1 answer

How to debug py.test in PyCharm when coverage is enabled

How do I debug py.test in PyCharm when coverage is enabled? Coverage is enabled using --cov=project --cov-report=term-missing, removing this and breakpoints are hit. Versions: pycharm 5.0.3, pytest==2.8.5, pytest-cache==1.0, pytest-cov==2.2.0,…
simonzack
  • 19,729
  • 13
  • 73
  • 118
25
votes
7 answers

Pytest - no tests ran

I'm using pytest and selenium. When I try run my test script: import pytest from selenium import webdriver from pages import * from locators import * from selenium.webdriver.common.by import By import time class RegisterNewInstructor: def…
Rafael C.
  • 2,245
  • 4
  • 29
  • 45
25
votes
5 answers

pytest capsys: checking output AND getting it reported?

Python 3.4.1, pytest 2.6.2. When a test fails, pytest will routinely report what was printed to stdout by the test. For instance this code: def method_under_test(): print("Hallo, Welt!") return 41 def test_result_only(): result =…
Lutz Prechelt
  • 36,608
  • 11
  • 63
  • 88
24
votes
2 answers

How to test a Connexion/Flask app?

I'm using the Connexion framework for Flask to build a microservice. I would like to write tests for my application using py.test. In the pytest-flask doc it says to create a fixture in conftest.py that creates the app like so: conftest.py import…
Sebastian Wozny
  • 16,943
  • 7
  • 52
  • 69
24
votes
3 answers

PyTest: skip entire module/file (python 2 and 3)

I want to skip an entire test if imports don't work: try: import networkx except: import pytest pytest.skip() This works fine with python-2.7 and python-3.5, both using pytest-2.8.1. When I try to run the code in python-3.6 with pytest-3.0.5…
mattmilten
  • 6,242
  • 3
  • 35
  • 65
24
votes
2 answers

How to patch globally in pytest?

I use pytest quite a bit for my code. Sample code structure looks like this. The entire codebase is…
Ranjith Ramachandra
  • 10,399
  • 14
  • 59
  • 96
24
votes
1 answer

pytest dynamically generate test method

Hi How can i generate test method dynamically for a list or for number of files. Say I have file1,file2 and filen with input value in json. Now I need to run the same test for multiple values like below, class Test_File(unittest.TestCase): def…
Naggappan Ramukannan
  • 2,564
  • 9
  • 36
  • 59
24
votes
1 answer

How do I test exceptions and errors using pytest?

I have functions in my Python code that raise exceptions in response to certain conditions, and would like to confirm that they behave as expected in my pytest scripts. Currently I have def test_something(): try: my_func(good_args) …
orome
  • 45,163
  • 57
  • 202
  • 418
24
votes
2 answers

Verify the error code or message from SystemExit in pytest

According to the pytest documentation, I can assert that a SystemExit() occurs, but I want to do more: I also want to verify the exit code and any messages. I tried the below code, but nothing prints and I'm not sure what I would need to assert to…
Robin Siebler
  • 263
  • 1
  • 2
  • 6
24
votes
5 answers

py.test to test flask register, AssertionError: Popped wrong request context

I'm using flask to do register and login: from flask.ext.security.views import register, login class Register(Resource): def post(self): return register() class Login(Resource): def post(self): return…
Spirit
  • 603
  • 2
  • 8
  • 16
24
votes
2 answers

Using Python PuDB debugger with pytest

Before my testing library of choice was unittest. It was working with my favourite debugger - PuDB. Not Pdb!!! To use PuDB with unittest, I paste import pudb;pudb.set_trace() between the lines of code. I then executed python -m unittest…
yagger
  • 2,975
  • 1
  • 16
  • 21
24
votes
1 answer

using pytest.raises to catch expected custom error

I am new to pytest and am trying to convert some functional test scripts into ones that play nicely with pytest. My module has custom error types and I'm trying to use the with pytest.raises() as excinfo method. This is a scientific/numerical…
user2097190
  • 241
  • 1
  • 2
  • 4
23
votes
3 answers

How to test async function using pytest?

@pytest.fixture def d_service(): c = DService() return c # @pytest.mark.asyncio # tried it too async def test_get_file_list(d_service): files = await d_service.get_file_list('') print(files) However, it got the following…
ca9163d9
  • 27,283
  • 64
  • 210
  • 413
23
votes
1 answer

can pytest ignore a specific warning?

I'd like pytest to ignore the RemovedInDjango20Warning. I currently have this in pytest.ini [pytest] filterwarnings = ignore:RemovedInDjango20Warning And that allows pytest to run, but does not suppress the warnings. If I add a second colon, I…
Chris Curvey
  • 9,738
  • 10
  • 48
  • 70