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

Patching a function inside a package __init__ and use it within a module inside the same package

I am trying to patch an internal package function defined in the __init__.py that is being used by a function inside of a module inside of the same package. The problem that I am facing is that it does not get patched. I've had some success when…
blastbeat
  • 43
  • 3
4
votes
3 answers

pytest - Patching a class does not work, calls class instead

Not sure why but here is my code snippet : stats_collector.py class StatsCollector( object ) : def __init__( self, user_id, app_id ) : logging.info( "Stats: APP_ID = {0}, ID = {1}".format( app_id, user_id ) ) mydb.py from…
ghostrider
  • 5,131
  • 14
  • 72
  • 120
4
votes
1 answer

How to debug using stdout during Pytest execution

I am trying to debug a feature of a test (that runs on pytest) I am setting a debug point and stop there (using Pycharm) However, I cannot debug efficiently the code because my stdout and stderr are redirected since this is a pytest. I tried…
ntg
  • 12,950
  • 7
  • 74
  • 95
4
votes
2 answers

Run a single file in pytest that is a module in a PEP 420 implicit namespace package

I have a file dir/subdir/module.py using relative imports so that python3 dir/subdir/module.py fails with ModuleNotFoundError: No module named '__main__.test_forward'; '__main__' is not a package but python3 -m…
Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
4
votes
3 answers

Test if __name__ == "__main__": with click and pytest

I have the following code in test.py: import click @click.command() @click.option('--text', default='hello world', help='Text to display.') def say(text): print(text) if __name__ == "__main__": say() If I call this in the command line,…
Nicolas Berthier
  • 459
  • 1
  • 8
  • 17
4
votes
1 answer

Unit Test mocking AWS Lambda Function, Python

I have a aws lambda function which will upload a dicom image located in an s3 bucket to a 3rd party server and retrieved the response. How do I mock my lambda function using pytest mock ?
Sarga
  • 149
  • 3
  • 16
4
votes
2 answers

How to pass an environment variable in command line to pytest to test a function

I have a script using os.environ.get() to get variable from command line something like JENKINS_HOST="xx" JENKINS_AUTH="xx" JENKINS_TOKEN="xx" python script.py In script.py has a function it likes this: def init_auth(): login_mode = True …
Saluton
  • 117
  • 1
  • 2
  • 9
4
votes
1 answer

How to use a fixture in a pytest skipif marker?

I have a py.test function like the follows: def my_test(self, driver): # Do something with the driver which uses a driver fixture (defined in conftest.py). Now, I need to check something before running this test. If that check fails, I need to…
Alex
  • 41,580
  • 88
  • 260
  • 469
4
votes
1 answer

How to run tests with tox.ini

I am reading and trying to understand some libraries online, and I come across the following: Tests with no pytest or unitest I am reading online and I found a tox.ini file like the following: [tox] envlist = py27 py35 py36 py37 …
may
  • 1,073
  • 4
  • 14
  • 31
4
votes
4 answers

How to run a pytest-bdd test?

I am not understanding how to properly run a simple test(feature file and python file) with the library pytest-bdd. From the official documentation, I can't understand what command to issue to run a test. I tried using pytest command, but I saw the…
Shamsul Arefin
  • 1,771
  • 1
  • 21
  • 21
4
votes
4 answers

How do I make custom comparisons in pytest?

For example I'd like to assert that two Pyspark DataFrame's have the same data, however just using == checks that they are the same object. Ideally I'd also like to be specify whether order matters or not. I've tried writing a function that raises…
kfoley
  • 320
  • 1
  • 9
4
votes
1 answer

Why can't pytest-django find manage.py?

I have a project structure like this: setup.cfg integration_tests/ |----tests.py src/ |----manage.py |----my_django_app/ And a setup.cfg with this: [tool:pytest] addopts=--tb=short --strict -ra DJANGO_SETTINGS_MODULE =…
fildred13
  • 2,280
  • 8
  • 28
  • 52
4
votes
1 answer

Calling function that yields from a pytest fixture

In my unit tests, I have two very similar fixtures, and I was hoping to break out some of the functionality into a helper function of some kind. Given my understanding of how yield produces generators, I don't feel like this should cause any kind of…
ollien
  • 4,418
  • 9
  • 35
  • 58
4
votes
0 answers

Any recommended way to manage pytest tests in a dockerized flask + postgres app?

I am writing an app with Docker, Flask and Postgres. I currently have one docker-compose.yml file for development and one for testing. The main objective is to use different databases. I could also have one docker compose file with 2 databases, one…
jul
  • 36,404
  • 64
  • 191
  • 318
4
votes
0 answers

Django - Pytest setup crash after deleting model

After deleting an old Model in my Django micro-service, pytest fails to setup the test DB. The migrations fail to execute correctly. However, the migration generated after the model deletion works well on my staging DB. The problem is only…