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
31
votes
1 answer

Mock exception raised in function using Pytest

I have the following function and it is a generic function which will make API call based on the input hostname and data. It will construct http request to make API and will return the response. This function will throw four types of…
Abheena Ps
  • 311
  • 1
  • 3
  • 3
31
votes
3 answers

Does pytest have an assertItemsEqual / assertCountEqual equivalent

unittest.TestCase has an assertCountEqual method (assertItemsEqual in Python 2, which is arguably a better name), which compares two iterables and checks that they contain the same number of the same objects, without regard for their order. Does…
Daisy Leigh Brenecki
  • 7,571
  • 6
  • 28
  • 43
31
votes
5 answers

How to save pytest's results/logs to a file?

I am having trouble trying to save -all- of the results shown from pytest to a file (txt, log, doesn't matter). In the test example below, I would like to capture what is shown in console into a text/log file of some sort: import pytest import…
nonbot
  • 873
  • 2
  • 9
  • 15
31
votes
5 answers

how to share a variable across modules for all tests in py.test

I have multiple tests run by py.test that are located in multiple classes in multiple files. What is the simplest way to share a large dictionary - which I do not want to duplicate - with every method of every class in every file to be used by…
Trevor
  • 1,613
  • 3
  • 22
  • 34
31
votes
5 answers

py.test logging control

We have recently switched to py.test for python testing (which is fantastic btw). However, I'm trying to figure out how to control the log output (i.e. the built-in python logging module). We have pytest-capturelog installed and this works as…
jjh
  • 554
  • 1
  • 5
  • 12
30
votes
7 answers

pytest: How to get a list of all failed tests at the end of the session? (and while using xdist)

I would like to have a list of all the tests that have failed to be used at the end of session. Pytest lets you define a hook pytest_sessionfinish(session, exitstatus), that is called at the end of the session, where I wish to have that list.…
Itay
  • 335
  • 1
  • 3
  • 6
30
votes
1 answer

Preventing pytest from creating .cache directories in Pycharm

I'm using Pycharm for this years Advent of Code and I'm using pytest for testing all of the examples and output. I'd prefer it if pytest didn't create the .cache directories throughout my directory tree. Is there anyway to disable the creation of…
labarna
  • 668
  • 1
  • 9
  • 16
30
votes
5 answers

Pytest - how to skip tests unless you declare an option/flag?

I have some unit tests, but I'm looking for a way to tag some specific unit tests to have them skipped unless you declare an option when you call the tests. Example: If I call pytest test_reports.py, I'd want a couple specific unit tests to not be…
Louis
  • 1,123
  • 5
  • 15
  • 24
30
votes
5 answers

Returning multiple objects from a pytest fixture

I am learning how to use pytest by testing a simple event emitter implementation. Basically, it looks like this class EventEmitter(): def __init__(self): ... def subscribe(self, event_map): # adds listeners to provided in…
Zallin
  • 435
  • 1
  • 4
  • 5
30
votes
2 answers

How to run specific code after all tests are executed?

I would like to run specific code after all the tests are executed using pytest for eg: I open up a database connection before any tests are executed. And I would like to close the connection after all the tests are executed. How can i achieve this…
sridhar249
  • 4,471
  • 6
  • 26
  • 27
30
votes
1 answer

How to mark test as xfail only with specific parameters with Pytest

I'm quite new to pytest and I would like to know how to mark a test as "expected to fail" when called with certain parameters. I parametrize test like this: @pytest.mark.parametrize("param1", [False, True]) @pytest.mark.parametrize("param2", [1, 2,…
user1967718
  • 835
  • 1
  • 13
  • 22
30
votes
3 answers

Py.Test : Reporting and HTML output

This is not a technical question at all really. However, I can not locate my .HTML report that is supposed to be generated using: py.test --cov-report html pytest/01_smoke.py I thought for sure it would place it in the parent location, or the test…
Dave
  • 1,031
  • 7
  • 19
  • 29
30
votes
5 answers

How to make pytest run doctests as well as normal tests directory?

We currently have pytest with the coverage plugin running over our tests in a tests directory. What's the simplest way to also run doctests extracted from our main code? --doctest-modules doesn't work (probably since it just runs doctests from…
Yang
  • 16,037
  • 15
  • 100
  • 142
29
votes
5 answers

How to assert that a type equals a given value

I am writing a test for a method and I want to validate that the method returns a specific type. However, when I try this I get an error. def search_emails(mail): data = mail.uid('search') raw_email = data[0][1] return raw_email The…
analyticsPierce
  • 2,979
  • 9
  • 57
  • 81
29
votes
2 answers

Why python's monkeypatch doesn't work when importing a class instead of a module?

I was having issues while using the code of the accepted answer here. The code works depending on how I do the import of datetime. Why is that? Is it possible to mock it so it works both ways? I am using Python 3.4. The following code illustrates…
rgargente
  • 1,821
  • 2
  • 19
  • 30