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
0 answers

ImportError: cannot import name when running pytest

My directory structure is like so: ├── person └── __init__.py └── person.py └── tests └── test_person.py ├── setup.py setup.py: from setuptools import setup, find_packages setup(name="person",…
raphattack
  • 179
  • 4
  • 12
4
votes
0 answers

Python Pytest - Project structure and import statements

Assuming the following standard project structure - project -- src ---- __init__.py ---- script1.py ---- script2.py ---- module ---- __init__.py ------ helper.py -- test ---- __init__.py ---- test_script1.py ---- test_script2.py ---- module ----…
4
votes
1 answer

Problem when using py.test with a pytorch project via the python extension of visual studio code

I'm working on a pytorch project using visual studio code and trying to use py.test. However, when trying to discover (or run) tests using the vs-code extension, I get an error (see log) importing torch. When I run pytest from the terminal,…
hdkrgr
  • 1,666
  • 1
  • 12
  • 22
4
votes
3 answers

Use own package in tox+pytest

I have python application which I would like to test. This application is importing a lot of packages for example: import time import sys . . . import mypackage Where mypackage is my own package. When I start application everything works fine.…
4
votes
1 answer

Running a pytest test from CMake where the test and sources are in different folders

I have a CMake based project that uses both C++ and python sources. Tests are added via add_test for all C++ tests in a folder structure like: src mynamespace/foo.cpp mypyspace/mypkg/__init__.py mypyspace/mypkg/bar.py test …
Flamefire
  • 5,313
  • 3
  • 35
  • 70
4
votes
1 answer

Skip test from inside a fixture

Let's say I have a fixture that requires a live database. If the live database doesn't exist, I want to skip tests that depend on that fixture. At the moment, I have to manually mark tests to skip, which feels redundant: @pytest.fixture def…
scribu
  • 2,958
  • 4
  • 34
  • 44
4
votes
1 answer

force pytest.main() to use a different pytest.ini

Is there a way to point pytest at a differnt pytest.ini file? The application I am working on has one pytest.ini file that is used for running unittests when it spins up, and I do not want to modify that file. I do however want to point pytest at…
Lombax
  • 851
  • 4
  • 9
  • 25
4
votes
1 answer

When to use a fixture in pytest as opposed to just declaring a variable

Given a test file as follows... (taken from the example at https://docs.pytest.org/en/latest/fixture.html) test_with_fixture.py import pytest @pytest.fixture def smtp_connection(): import smtplib return smtplib.SMTP("smtp.gmail.com", 587,…
Remotec
  • 10,304
  • 25
  • 105
  • 147
4
votes
1 answer

py.test parameterized fixtures

I am currently working on some unit test cases and test fixtures using py.test I have some code that does this: # my py.test file import pytest @pytest.fixture def fixture_1(): f = open("file_one.txt", 'rb') f_str = f.read() yield…
pranav3688
  • 694
  • 1
  • 11
  • 20
4
votes
1 answer

Using pytest when tests are in a separate folder

my python folder structure is as follows repository -libraries -image -imagefuncs.py -videos -videofuncs.py -text -textfuncs.py -docs -tests -test_imagefuncs.py For the life of me i cannot figure…
Thalish Sajeed
  • 1,351
  • 11
  • 25
4
votes
1 answer

Pytest fixture: setup, teardown and code running between each test

I am trying to use pytest to test a module I am writing. The module is a wrapper of a process with a LONG startup time. I, therefore, want to make sure I have a proper setup/teardown logic to make sure the initialization does not happen more than…
hirolau
  • 13,451
  • 8
  • 35
  • 47
4
votes
0 answers

How can I unit test code which uses a confluent_kafka Consumer?

I'm trying to unit test the following piece of code using pytest: import json from typing import Any, Dict from confluent_kafka import Consumer def get_message(config: Dict[str, Any]): consumer = Consumer( { "group.id":…
Pitirus
  • 117
  • 1
  • 13
4
votes
3 answers

How to run pytest with a specified test directory on a file that uses argparse?

I am writing unit tests for a Python library using pytest I need to specify a directory for test files to avoid automatic test file discovery, because there is a large sub-directory structure, including many files in the library containing "_test"…
Jake Levi
  • 1,329
  • 11
  • 16
4
votes
1 answer

pytest --help does not run when using pytest_configure() in conftest.py

update the question and subject as I discovered more. Without conftest.py "pytest --help" returns help content. With the conftest.py "pytest --help" returns this INTERNALERROR> Traceback (most recent call last): INTERNALERROR> File…
Soprano86
  • 73
  • 2
  • 10
4
votes
2 answers

How can pytest-cov report coverage of python code that is executed as a result of pexpect.spawn?

I have a Python project that uses pytest-cov for unit testing and code coverage measurement. The directory structure for my project is: rift-python +- rift # The package under test | +- __init__.py | +- __main__.py | +-…
Bruno Rijsman
  • 3,715
  • 4
  • 31
  • 61
1 2 3
99
100