conftest.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # -*- coding: utf-8 -*-
  2. """Helpers for testing."""
  3. import io
  4. import os
  5. import pytest
  6. DIR_PATH = os.path.dirname(__file__)
  7. FILES_DIR = os.path.join(DIR_PATH, 'files')
  8. @pytest.fixture()
  9. def filepath():
  10. """Returns full file path for test files."""
  11. def make_filepath(filename):
  12. # http://stackoverflow.com/questions/18011902/parameter-to-a-fixture
  13. # Alternate solution is to use paramtrization `inderect=True`
  14. # http://stackoverflow.com/a/33879151
  15. # Syntax is noisy and requires specific variable names
  16. return os.path.join(FILES_DIR, filename)
  17. return make_filepath
  18. @pytest.fixture()
  19. def load_file(filepath):
  20. """Opens filename with encoding and return its contents."""
  21. def make_load_file(filename, encoding='utf-8'):
  22. # http://stackoverflow.com/questions/18011902/parameter-to-a-fixture
  23. # Alternate solution is to use paramtrization `inderect=True`
  24. # http://stackoverflow.com/a/33879151
  25. # Syntax is noisy and requires specific variable names
  26. # And seems to be limited to only 1 argument.
  27. with io.open(filepath(filename), encoding=encoding) as f:
  28. return f.read()
  29. return make_load_file