1
- import pytest
1
+ from __future__ import annotations
2
+
2
3
import textwrap
3
4
4
- def test_discover_imports_enabled (pytester ):
5
+ from _pytest .pytester import Pytester
6
+
7
+
8
+ def run_import_class_test (pytester : Pytester , passed : int = 0 , errors : int = 0 ) -> None :
5
9
src_dir = pytester .mkdir ("src" )
6
10
tests_dir = pytester .mkdir ("tests" )
7
- pytester .makeini ("""
8
- [pytest]
9
- testpaths = "tests"
10
- discover_imports = true
11
- """ )
12
-
13
11
src_file = src_dir / "foo.py"
14
12
15
- src_file .write_text (textwrap .dedent ("""\
16
- class TestClass(object):
13
+ src_file .write_text (
14
+ textwrap .dedent ("""\
15
+ class Testament(object):
17
16
def __init__(self):
18
17
super().__init__()
18
+ self.collections = ["stamp", "coin"]
19
19
20
- def test_foobar(self):
21
- return true
22
- """
23
- ), encoding = "utf-8" )
20
+ def personal_property(self):
21
+ return [f"my {x} collection" for x in self.collections]
22
+ """ ),
23
+ encoding = "utf-8" ,
24
+ )
24
25
25
26
test_file = tests_dir / "foo_test.py"
26
- test_file .write_text (textwrap .dedent ("""\
27
+ test_file .write_text (
28
+ textwrap .dedent ("""\
27
29
import sys
28
30
import os
29
31
@@ -32,42 +34,78 @@ def test_foobar(self):
32
34
parent_dir = os.path.abspath(os.path.join(current_dir, '..'))
33
35
sys.path.append(parent_dir)
34
36
35
- from src.foo import TestClass
37
+ from src.foo import Testament
36
38
37
39
class TestDomain:
38
40
def test_testament(self):
39
- testament = TestClass()
40
- pass
41
- """ ), encoding = "utf-8" )
41
+ testament = Testament()
42
+ assert testament.personal_property()
43
+ """ ),
44
+ encoding = "utf-8" ,
45
+ )
42
46
43
47
result = pytester .runpytest ()
44
- result .assert_outcomes (errors = 1 )
48
+ result .assert_outcomes (passed = passed , errors = errors )
45
49
46
- def test_discover_imports_disabled (pytester ):
47
-
48
- src_dir = pytester .mkdir ("src" )
49
- tests_dir = pytester .mkdir ("tests" )
50
+
51
+ def test_collect_imports_disabled (pytester : Pytester ) -> None :
52
+ pytester .makeini ("""
53
+ [pytest]
54
+ testpaths = "tests"
55
+ collect_imported_tests = false
56
+ """ )
57
+
58
+ run_import_class_test (pytester , errors = 1 )
59
+
60
+
61
+ def test_collect_imports_default (pytester : Pytester ) -> None :
62
+ pytester .makeini ("""
63
+ [pytest]
64
+ testpaths = "tests"
65
+ """ )
66
+
67
+ run_import_class_test (pytester , errors = 1 )
68
+
69
+
70
+ def test_collect_imports_enabled (pytester : Pytester ) -> None :
50
71
pytester .makeini ("""
51
72
[pytest]
52
73
testpaths = "tests"
53
- discover_imports = false
74
+ collect_imported_tests = true
54
75
""" )
55
76
77
+ run_import_class_test (pytester , passed = 1 )
78
+
79
+
80
+ def run_import_functions_test (
81
+ pytester : Pytester , passed : int , errors : int , failed : int
82
+ ) -> None :
83
+ src_dir = pytester .mkdir ("src" )
84
+ tests_dir = pytester .mkdir ("tests" )
85
+
56
86
src_file = src_dir / "foo.py"
57
87
58
- src_file .write_text (textwrap .dedent ("""\
59
- class Testament(object):
60
- def __init__(self):
61
- super().__init__()
62
- self.collections = ["stamp", "coin"]
88
+ # Note that these "tests" are should _not_ be treated as tests.
89
+ # They are normal functions that happens to have test_* or *_test in the name.
90
+ # Thus should _not_ be collected!
91
+ src_file .write_text (
92
+ textwrap .dedent ("""\
93
+ def test_function():
94
+ some_random_computation = 5
95
+ return some_random_computation
63
96
64
- def personal_property(self):
65
- return [f"my {x} collection" for x in self.collections]
66
- """
67
- ), encoding = "utf-8" )
97
+ def test_bar():
98
+ pass
99
+ """ ),
100
+ encoding = "utf-8" ,
101
+ )
68
102
69
103
test_file = tests_dir / "foo_test.py"
70
- test_file .write_text (textwrap .dedent ("""\
104
+
105
+ # Inferred from the comment above, this means that there is _only_ one actual test
106
+ # which should result in only 1 passing test being ran.
107
+ test_file .write_text (
108
+ textwrap .dedent ("""\
71
109
import sys
72
110
import os
73
111
@@ -76,13 +114,37 @@ def personal_property(self):
76
114
parent_dir = os.path.abspath(os.path.join(current_dir, '..'))
77
115
sys.path.append(parent_dir)
78
116
79
- from src.foo import Testament
117
+ from src.foo import *
80
118
81
119
class TestDomain:
82
- def test_testament(self):
83
- testament = Testament()
84
- assert testament.personal_property()
85
- """ ), encoding = "utf-8" )
120
+ def test_important(self):
121
+ res = test_function()
122
+ if res == 5:
123
+ pass
124
+
125
+ """ ),
126
+ encoding = "utf-8" ,
127
+ )
86
128
87
129
result = pytester .runpytest ()
88
- result .assert_outcomes (passed = 1 )
130
+ result .assert_outcomes (passed = passed , errors = errors , failed = failed )
131
+
132
+
133
+ def test_collect_function_imports_enabled (pytester : Pytester ) -> None :
134
+ pytester .makeini ("""
135
+ [pytest]
136
+ testpaths = "tests"
137
+ collect_imported_tests = true
138
+ """ )
139
+
140
+ run_import_functions_test (pytester , passed = 1 , errors = 0 , failed = 0 )
141
+
142
+
143
+ def test_collect_function_imports_disabled (pytester : Pytester ) -> None :
144
+ pytester .makeini ("""
145
+ [pytest]
146
+ testpaths = "tests"
147
+ collect_imported_tests = false
148
+ """ )
149
+
150
+ run_import_functions_test (pytester , passed = 2 , errors = 0 , failed = 1 )
0 commit comments