Coverage for tests/conftest.py: 100%

32 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2024-08-27 18:25 +0000

1from __future__ import annotations 

2 

3import os 

4import sys 

5from typing import TYPE_CHECKING, List, Iterator 

6 

7import pytest 

8 

9import prisma 

10from prisma import Prisma 

11from prisma.cli import setup_logging 

12from lib.testing.shared_conftest import * 

13from lib.testing.shared_conftest.async_client import * 

14 

15from .utils import Runner, Testdir 

16 

17if TYPE_CHECKING: 

18 from _pytest.config import Config 

19 from _pytest.pytester import Pytester 

20 from _pytest.monkeypatch import MonkeyPatch 

21 

22 

23LOGGING_CONTEXT_MANAGER = setup_logging(use_handler=False) 

24 

25 

26prisma.register(Prisma()) 

27 

28pytest.register_assert_rewrite('tests.test_generation.utils') 

29 

30 

31@pytest.fixture() 

32def runner(monkeypatch: 'MonkeyPatch') -> Runner: 

33 """Fixture for running cli tests""" 

34 return Runner(patcher=monkeypatch) 

35 

36 

37@pytest.fixture(name='testdir') 

38def testdir_fixture(pytester: Pytester) -> Iterator[Testdir]: 

39 cwd = os.getcwd() 

40 os.chdir(pytester.path) 

41 sys.path.insert(0, str(pytester.path)) 

42 

43 yield Testdir(pytester) 

44 

45 os.chdir(cwd) 

46 sys.path.remove(str(pytester.path)) 

47 

48 

49# TODO: don't emulate the with statement 

50def pytest_sessionstart(session: pytest.Session) -> None: 

51 LOGGING_CONTEXT_MANAGER.__enter__() 

52 

53 

54def pytest_sessionfinish(session: pytest.Session) -> None: 

55 if ( # pragma: no branch 

56 LOGGING_CONTEXT_MANAGER is not None 

57 ): # pyright: ignore[reportUnnecessaryComparison] 

58 LOGGING_CONTEXT_MANAGER.__exit__(None, None, None) 

59 

60 

61def pytest_collection_modifyitems(session: pytest.Session, config: Config, items: List[pytest.Item]) -> None: 

62 items.sort(key=lambda item: item.__class__.__name__ == 'IntegrationTestItem')