How can I make test data files accessible to pytest tests when run with tox?

Viewed 4633

I want to run a test for a function which accepts a Path to a file as input via an argument: function(some_path_to_file) via tox. The file I want to pass to the function cannot be created temporarily during test setup (what I usually do via pytests builtin tmpdir fixtures) but resides in the package <package>/data directory besides the test directory <package>/tests (the location <package>/tests/data would probably be better). Because tox runs the tests in a virtualenv it's not clear to me how to make the test data file available to the test. I know that I can define the base temporary directory of pytest with the --basedir option but I did not get it working with tox yet.

tl;dr

The problem was a conversion of some_path_to_file from Path to str (to pass it to sqlite3.connect(database inside the function) using Path.resolve(. No need to configure pytests --basedir option and tox in any way.

2 Answers

This tripped me up as well. The way I was able to solve it was to specify the full path of the text file I wanted the testing function to read relative to the base directory.

So for example, my directory tree looks like this:

.
├── __init__.py
├── my_package
│   ├── __init__.py
│   └── calculate_stats.py
├── my_package.egg-info
│   ├── PKG-INFO
│   ├── SOURCES.txt
│   ├── dependency_links.txt
│   ├── requires.txt
│   └── top_level.txt
├── bin
│   └── calculate_stats
├── requirements
│   ├── default.txt
│   └── development.txt
├── setup.py
├── test
│   ├── __init__.py
│   ├── test_calculate_stats.csv
│   ├── test_calculate_stats.txt
│   └── test_calculate_stats.py
└── tox.ini

In the file test_calculate_stats.py I have the following line:

assert (calculate_stats.calculate_stats_to_csv("test/test_calculate_stats.txt", "test/test_calculate_stats.csv") == 60)

The calculate_stats_to_csv function reads in the test/test_calculate_stats.txt file, calculates some stats, and outputs them to test/test_calculate_stats.csv

Initially I had just specified the input file to be test_calculate_stats.txt because it's in the same directory as the file containing the testing function -- that's when I ran into the error.

tox predfines a number of substitutions. The directory of the virtualenv is {envdir}, site-packages is at {envsitepackagesdir}. Pass a value from the command line to your test script like this:

[testenv]
    commands = pytest --basedir={envsitepackagesdir}/mypackage
Related