Skip to content

Fixture

data path fixture

import pytest
from pathlib import Path

DATA_PATH = Path(__file__).absolute().parent.joinpath('data')

@pytest.fixture
def data_path():
    return DATA_PATH

def test_data_path(data_path: Path):
    ...

call fixture function directly

solution: https://github.com/pytest-dev/pytest/issues/3950

  • bad - default argument: this makes the fixture run at import time, not at test runtime

    def data():
        ...
    def test_my_func(data=data()):
        ...
    
  • extract fixture

      def _data():
          ...
      @pytest.fixture
      def data():
        return _data()
    
  • manual wrapping

    def _data():
        ...
    data = pytest.fixture(_data) # attach fixture to pytest
    
  • direct invocation: @pytest.fixture keeps the original function in .__wrapped__

    if __name__ == '__main__':
        test_my_func(data=data.__wrapped__())