This is what my project looks like (oversimplified): Whenever I run pytest standing in root/ I get ImportError : Attempted relative import with no known parent package. If I move test_code.py out to root/ everything works fine. But this is not an option since I have many modules and that is why I need to put ALL test modules inside
Tag: pytest
Using SQLAlchemy 1.4 classical / imperative mapping style?
I am using the clean architecture & TDD development method in my Python project. Updating from SQLAlchemy 1.3 to SQLAlchemy 1.4 broke the ability to test against an in-memory Postgres DB, and I can’t find how to fix the problem. Following DDD principles, the project uses the new imperative mapping syntax which replace classical mapping declarations. Here is a minimal
Unable to mock os name with Python on Windows
I have the following method: I’m trying to test it like this: But then the following error is raised: What is happening exactly? Answer What is happening here is that pytest internally uses a pathlib.Path object, which upon initialization asks for os.name to define which Path implementation to use. There are two internal implementations for Path, PosixPath and WindowsPath, which
Pytest: How to locate a FutureWarning and fix it?
In my current project when I run my tests (with pytest) I get this output (besides others): This FutureWarning is quite clear – from version 1.0 (I’m using 0.24 at the moment) there will be change to some parameters and I will not be able to pass standardize as positional arguments. But the question is – How can I locate
How to conditionally skip instantiation of fixture in pytest?
Problem: I have a fixture that takes about 5 minutes to instantiate. This fixture relies on a fixture from another package that I cannot touch. The time of the fixture can be drastically sped up depending on the state of a different (must faster instantiating) fixture. For example, this is the psuedo code of what I am looking to do:
How to skip a test in pytest *before* fixtures are computed
I have a fairly large test suite written with pytest which is meant to perform system tests on an application that includes communication between server side and client side. The tests have a huge fixture which is initialized to contain information about the server which is then used to create a client object and run the tests. The server side
Exclude some tests by default
I wish to configure pytest such that it excludes some tests by default; but it should be easy to include them again with some command line option. I only found -k, and I have the impression that that allows complex specifications, but am not sure how to go about my specific need… The exclusion should be part of the source
Pytest: Nested use of request fixture
I’d like to get some help on how to run pytests with multiple layers of parameterized pytest fixtures. I have a global request-based fixture for selecting a base implementation of the system I’d like to test: I’d like to now run some additionally-parametrized tests against different test cases on the backend. Using these two layers of fixtures, I’d like to
pytest breaks with pip install
I’m working in a repo that has tests and some test helper libraries in a directory named test (https://github.com/covid-projections/covid-data-model/tree/main/test). Mysteriously pip install dash seems to break our tests. I’m using python 3.7.9 in pyenv on a debian laptop. I reproduced the problem with a pretty simple example: Activate a fresh pyenv virtualenv and install the latest pytest with pip install
Import conftest.py from peer package
Currently, I have two packages as part of my test repository with the following folder structure: As part of this, my requirement is to call fixtures defined in Package_A/conftest.py from Package_B/test_B.py. I understand that this could be done easily if Package B was within Package A. However, because of certain requirements that is not possible. Is there a way I