Debian Codegraph @

tests/test_pytest_cython.py

raw
1 import pathlib
2
3 # Check imports to ensure packages are installed before running tests
4 import pypackage # noqa: F401
5 import pytest
6
7 ROOT_PATH = pathlib.Path(__file__).parent
8 PROJECT_PATH = ROOT_PATH.joinpath("example-project")
9 PACKAGE_PATH = PROJECT_PATH.joinpath("src", "pypackage")
10
11 # TODO: Figure out if importlib can be supported.
12 IMPORT_MODES = ["append", "prepend"]
13
14
15 def get_module(basename: str, suffix=".pyx") -> pathlib.Path:
16 return PACKAGE_PATH.joinpath(basename + suffix)
17
18
19 def run_pytest(
20 pytester: pytest.Pytester, module: pathlib.Path, import_mode
21 ) -> pytest.RunResult:
22 return pytester.runpytest(
23 "-vv",
24 "--doctest-modules",
25 "--doctest-cython",
26 "--import-mode",
27 import_mode,
28 str(module),
29 )
30
31
32 @pytest.mark.parametrize("import_mode", IMPORT_MODES)
33 def test_cython_ext_module(pytester, import_mode):
34 module = get_module("cython_ext_module")
35 assert module.exists()
36
37 result = run_pytest(pytester, module, import_mode)
38 result.stdout.fnmatch_lines(
39 [
40 "*Eggs.__init__*PASSED*",
41 "*Eggs.blarg*PASSED*",
42 "*Eggs.failing_test*FAILED*",
43 "*Eggs.fubar*PASSED*",
44 "*",
45 "*FAILURES*",
46 "*pypackage.cython_ext_module.Eggs.failing_test*",
47 "078*",
48 "079 >>> eggs = Eggs(1, 1)*",
49 "080 >>> eggs.failing_test()*",
50 "Expected:*",
51 " False*",
52 "Got:*",
53 " True*",
54 ]
55 )
56 assert result.ret == 1
57
58
59 @pytest.mark.parametrize("import_mode", IMPORT_MODES)
60 def test_wrap_c_ext_module(pytester, import_mode):
61 module = get_module("wrap_c_ext_module")
62 assert module.exists()
63
64 result = run_pytest(pytester, module, import_mode)
65 result.stdout.fnmatch_lines(
66 [
67 "*sqr*PASSED*",
68 ]
69 )
70 assert result.ret == 0
71
72
73 @pytest.mark.parametrize("import_mode", IMPORT_MODES)
74 def test_wrap_cpp_ext_module(pytester, import_mode):
75 module = get_module("wrap_cpp_ext_module")
76 assert module.exists()
77
78 result = run_pytest(pytester, module, import_mode)
79 result.stdout.fnmatch_lines(
80 [
81 "*sqr*PASSED*",
82 ]
83 )
84 assert result.ret == 0
85
86
87 @pytest.mark.parametrize("import_mode", IMPORT_MODES)
88 def test_pure_py_module(pytester, import_mode):
89 module = get_module("pure_py_module", suffix=".py")
90 assert module.exists()
91
92 result = run_pytest(pytester, module, import_mode)
93 result.stdout.fnmatch_lines(
94 [
95 "*Eggs.__init__*PASSED*",
96 "*Eggs.foo*PASSED*",
97 "*foo*PASSED*",
98 ]
99 )
100 assert result.ret == 0