nteract / testbook

πŸ§ͺ πŸ“— Unit test your Jupyter Notebooks the right way

Home Page:https://testbook.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Because of project structure, relative paths in Notebook are no longer valid when testbook executes Notebook before tests

nakami opened this issue Β· comments

commented

Hello,
in my Jupyter Notebook I load data files via a pre-defined relative path. When I execute the Notebook in the normal fashion that path is valid. However, my Jupyter Notebook, my data set and my test file are in different directories. The execute parameter of testbook's decorator presumably executes the Notebook in the context of the current working directory pytest is called from or in the context of the test file. Therefore, my relative path of the Notebook is no longer valid when testbook executes the Notebook before running my test.

What would be considered good practice dealing with this problem?

Example of project structure:

project/
β”œβ”€β”€ notebooks/
β”‚   └── example.ipyn        # has a hard-coded path variable '../data/dataset.csv'
β”œβ”€β”€ data/
β”‚   └── dataset.csv
└── test/
    └── testbook_test.py

Executing the testbook test:

project> pytest test/testbook_test.py
[...]
E               FileNotFoundError: [Errno 2] No such file or directory: '../data/dataset.csv'

/opt/conda/lib/python3.10/site-packages/testbook/client.py:135: TestbookRuntimeError
commented

Update: I went with an obvious solution. It's possible to run pytest directly in the directory of my jupyter notebook. This way, my relative paths are resolved relatively to this folder.

Anyways, instead I'd rather like to either manipulate the execution's working directory or be able to override variables during the execution. Executing pytest from a different folder is working for me though, so I'll close this issue.