4

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.

thinwybk
  • 4,193
  • 2
  • 40
  • 76

2 Answers2

4

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.

Lina
  • 304
  • 2
  • 10
  • What have you passed to pytest via --basedir option in tox? – thinwybk Nov 16 '18 at 17:08
  • 1
    Nothing actually -- in `tox.ini` I have `commands = unit: pytest -v test` My `pytest` doesn't seem to recognize `--basedir`. The `pytest` I am using is version 4.0.0 and I'm running python v3.7.1 – Lina Nov 16 '18 at 17:28
  • In my specific use case I am connecting to a database in the function via [connect(database, ...](https://docs.python.org/3/library/sqlite3.html#sqlite3.connect) accepts a [path like object](https://docs.python.org/3/glossary.html#term-path-like-object). Previously I converted `Path` to `str` and passed it to `connect(database`. I changed the production code from `function(some_path_to_file: Path)` to `function(some_path_to_file: str)` and removed the conversion. The function reads the file correctly and the test runs. Still wondering why the Path to str conversion worked w.o. tox – thinwybk Nov 16 '18 at 18:57
  • ... figured it out: In the conversion of the file path `some_path_to_file` from `Path` to `str` I used `str(Path.resolve(...))` to get full path and pass it to `connect(` as `str`. If one uses relative path conversion `str(Path(...))` it works just fine. – thinwybk Nov 16 '18 at 19:07
0

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
phd
  • 82,685
  • 13
  • 120
  • 165