-1

I'm making a Flask REST API with Flask-RESTX library. One part of API are also calls that generate PDF documents. To generate documents I'm using some font files and images from the static/ directory. Everything is working when I'm running the API, but when I'm trying to call the same calls from my tests I'm getting an error that files used in PDF generation can't be found.

OSError: Cannot open resource "static/logo.png"

I'm guessing the reason for this is that the path to static folder is different when running tests. Is there a nice way to use the same path to static files in tests or there needs to be some custom path-switching logic depending on the run type (development, production, testing).

Folder structure:

/src
  /application
    /main
      app.py
    /test
      test.py
  /static
    logo.png

I access the assets in my application as such:

static/logo.png
Denis Vitez
  • 608
  • 11
  • 32

1 Answers1

0

So I solved the issue by changing the current working directory inside tests. Following this SO post. So I implemented the method:

from contextlib import contextmanager

@contextmanager
def cwd(path):
    old_pwd = os.getcwd()
    os.chdir(path)
    try:
        yield
    finally:
        os.chdir(old_pwd)

And then during my tests I then change the path during the test:

def simple_test():
    # default CWD
    with cwd('../../'):
        # code inside this block, and only inside this block, is in the new directory
        perform_call()
    # default CWD
Denis Vitez
  • 608
  • 11
  • 32