Skip to content

Commit cc7143b

Browse files
committed
Add sample of values returned by fixtures in test_fixtures.py
1 parent 5a90b1d commit cc7143b

3 files changed

Lines changed: 78 additions & 23 deletions

File tree

‎test_docs/conftest.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ def pytest_configure(config):
1717
config.addinivalue_line(
1818
"markers", "marker_name3 : marker test written into conftest.py for a better understanding"
1919
)
20+
config.addinivalue_line(
21+
"markers", "marker_name4 : marker test written into conftest.py for a better understanding"
22+
)
2023

2124

2225
@fixture(scope='session')
@@ -26,3 +29,12 @@ def I_handle_DB_URL_FILE_in_class():
2629
print(" --> disconnect from DB/URL/FILE in class", end=' ')
2730

2831

32+
@fixture(scope='function', params=[10, 20])
33+
def different_values_tested_in_param(request): # param and request are keywords
34+
print(f" --> fixture opened with {request.param}", end=' ')
35+
return request.param
36+
37+
@fixture(scope="class")
38+
def value_given_by_fixture(request):
39+
request.cls.input = [10,20]
40+

‎test_docs/test_fictures.py‎

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,9 @@
128128
test_fictures.py::check_02_TestCase_Tests::test_method_03 --> test_method_03 PASSED --> disconnect from DB/URL/FILE in class
129129
'''
130130

131-
# Fictures can also be added to the conftest.py file for handling the 'package' or 'session' scope once
132-
# for all the file of the directory
131+
132+
# Fixtures can be out or included into a class or upgrade to the conftest.py file if needed in different files
133+
# with the 'package' or 'session' scope
133134
#
134135
# in conftest.py :
135136
# @fixture(scope='session')
@@ -157,13 +158,28 @@ def test_method_03(self):
157158
print(" --> test_method_03", end=' ')
158159
assert True
159160

161+
@mark.marker_name4
162+
@mark.usefixtures('different_values_tested_in_param')
163+
def test_method_04(self, different_values_tested_in_param):
164+
print(f" --> test_method_04 says {different_values_tested_in_param} > 0", end=' ')
165+
assert different_values_tested_in_param > 0
166+
167+
@mark.marker_name4
168+
@pytest.mark.usefixtures("value_given_by_fixture")
169+
def test_method_05(self):
170+
print(f" --> test_method_05 says {self.input[1]} > 0", end=' ')
171+
assert self.input[1] > 0
172+
173+
174+
160175

161176
# Without unittest.TestCase heritage
162177
class check_01_Tests(repeating_calls): pass
163178

164-
165179
# With unittest.TestCase heritage
166-
class check_02_TestCase_Tests(unittest.TestCase, repeating_calls): pass
180+
class check_02_TestCase_Tests(unittest.TestCase, repeating_calls):pass
181+
182+
167183

168184
''' With the 'session' scope, the DB is opened only once at the beginning of all tests,
169185
even if they are in differents test files, and closed at the end
@@ -175,4 +191,38 @@ class check_02_TestCase_Tests(unittest.TestCase, repeating_calls): pass
175191
test_marks.py::check_04_TestCase_Tests::test_method_01 PASSED
176192
test_marks.py::check_04_TestCase_Tests::test_method_02 PASSED
177193
test_marks.py::check_04_TestCase_Tests::test_method_03 SKIPPED --> disconnect from DB/URL/FILE in class
194+
'''
195+
196+
# At least, fixtures can return values to test and be parametrized to test different values (not on TestCase)
197+
#
198+
# Use the request.cls.input to set a value or a dictionary like {'key':'value'}
199+
# @fixture(scope="class")
200+
# def value_given_by_fixture(request):
201+
# request.cls.input = [10,20]
202+
#
203+
# And the self.input option to get it into the test_method
204+
# @pytest.mark.usefixtures("value_given_by_fixture")
205+
# def test_method_05(self):
206+
# print(f" --> test_method_05 says {self.input[1]} > 0", end=' ')
207+
# assert self.input[1] > 0
208+
#
209+
# Or use the params option into the fixture and the request.param to return them one by one to the test_method
210+
# @fixture(scope='function', params=[10,20])
211+
# def value_in_parameter(request): # param and request are keywords
212+
# print(f" --> fixture opened with {request.param}", end=' ')
213+
# return request.param
214+
#
215+
# @mark.usefixtures('different_values_tested_in_param')
216+
# def test_method_04(self, different_values_tested_in_param):
217+
# print(f" --> test_method_04 says {different_values_tested_in_param} > 0", end=' ')
218+
# assert different_values_tested_in_param > 0
219+
#
220+
# But params is not supported by unittest.TestCase
221+
'''
222+
>>> pytest -v -k test_fictures.py -m 'marker_name4' -s
223+
test_fictures.py::check_01_Tests::test_method_04[10] --> fixture opened with 10 --> test_method_04 says 10 > 0 PASSED
224+
test_fictures.py::check_01_Tests::test_method_04[20] --> fixture opened with 20 --> test_method_04 says 20 > 0 PASSED
225+
test_fictures.py::check_01_Tests::test_method_05 --> test_method_05 says 20 > 0 PASSED
226+
test_fictures.py::check_02_TestCase_Tests::test_method_04 ERROR
227+
test_fictures.py::check_02_TestCase_Tests::test_method_05 --> test_method_05 says 20 > 0 PASSED
178228
'''

‎test_docs/test_marks.py‎

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ def test_method_02(self): assert (1 + 1 == 2) # 'mark_test' marked function
4444
@mark.skip('This is a skip reason : no need to test this method')
4545
def test_method_03(self): assert (1 + 1 == 2) # 'skip' marked function
4646

47-
# parametrize is not supported by unittest.TestCase
48-
@mark.marker_name3
49-
@mark.parametrize("val",[1,2]) # 'parametrize' may test different values
50-
def test_method_04(self, val):
51-
assert val > 0
47+
# # parametrize is not supported by unittest.TestCase
48+
# @mark.marker_name3
49+
# @mark.parametrize("val",[1,2]) # 'parametrize' may test different values
50+
# def test_method_04(self, val):
51+
# assert val > 0
5252

5353
# and should be modified by using parameterized (following this structure)
5454
@parameterized.expand([
@@ -57,7 +57,7 @@ def test_method_04(self, val):
5757
])
5858
@mark.marker_name3 # Careful, the parameterized must not be under marker
5959
def test_method_05(self,val1, val2):
60-
print(f"Test {val1} > 0 (no use of {val2})", end=' ')
60+
print(f"Test {val1} > 0 ({val2} not used here)", end=' ')
6161
assert val1 > 0 # 'parametrize' to test different values
6262

6363

@@ -77,13 +77,6 @@ class check_05_TestCase_Tests(unittest.TestCase,repeating_checks): pass # 'ma
7777
@mark.skip('This is a skip reason : no need to test this class')
7878
class check_06_TestCase_Tests(unittest.TestCase,repeating_checks): pass # 'skip' marked class
7979

80-
# # # With unittest.TestCase heritage
81-
# @mark.marker_name2
82-
# class check_04_TestCase_Tests(repeating_checks): pass
83-
# @mark.marker_name
84-
# class check_05_TestCase_Tests(repeating_checks): pass # 'mark_test' marked class
85-
# @mark.skip('This is a skip reason : no need to test this class')
86-
# class check_06_TestCase_Tests(repeating_checks): pass # 'skip' marked class
8780

8881
''' pytest considers all tests if pytest.ini file is defined
8982
>>> pytest -v -k test_marks.py
@@ -133,7 +126,7 @@ class check_06_TestCase_Tests(unittest.TestCase,repeating_checks): pass # 'sk
133126
>>> pytest -v -k test_marks.py -m 'not marker_name'
134127
test_marks.py::check_01_Tests::test_method_01 PASSED
135128
test_marks.py::check_03_Tests::test_method_01 PASSED
136-
test_marks.py::check_04_TestCase_Tesgits::test_method_01 PASSED
129+
test_marks.py::check_04_TestCase_Tests::test_method_01 PASSED
137130
test_marks.py::check_06_TestCase_Tests::test_method_01 PASSED
138131
'''
139132
''' WITHOUT pytest.ini file
@@ -194,7 +187,7 @@ class check_06_TestCase_Tests(unittest.TestCase,repeating_checks): pass # 'sk
194187
# ])
195188
# @mark.marker_name3 # Careful, the parameterized must not be under marker
196189
# def test_method_05(self,val1, val2):
197-
# print(f"Test {val1} > 0 (no use of {val2})", end=' ')
190+
# print(f"Test {val1} > 0 ({val2} not used here)", end=' ')
198191
# assert val1 > 0 # 'parametrize' to test different values
199192
#
200193
# This required to import and install 'parameterized'
@@ -203,9 +196,9 @@ class check_06_TestCase_Tests(unittest.TestCase,repeating_checks): pass # 'sk
203196
>>> pytest -v -k test_marks.py -m 'marker_name and marker_name3' -s
204197
test_marks.py::check_02_Tests::test_method_04[1] PASSED
205198
test_marks.py::check_02_Tests::test_method_04[2] PASSED
206-
test_marks.py::check_02_Tests::test_method_05_0 Test 1 > 0 (no use of 1) PASSED
207-
test_marks.py::check_02_Tests::test_method_05_1 Test 2 > 0 (no use of 2) PASSED
199+
test_marks.py::check_02_Tests::test_method_05_0 Test 1 > 0 (1 not used here) PASSED
200+
test_marks.py::check_02_Tests::test_method_05_1 Test 2 > 0 (2 not used here) PASSED
208201
test_marks.py::check_05_TestCase_Tests::test_method_04 FAILED # Not support by unittest.TestCase
209-
test_marks.py::check_05_TestCase_Tests::test_method_05_0 Test 1 > 0 (no use of 1) PASSED
210-
test_marks.py::check_05_TestCase_Tests::test_method_05_1 Test 2 > 0 (no use of 2) PASSED
202+
test_marks.py::check_05_TestCase_Tests::test_method_05_0 Test 1 > 0 (1 not used here) PASSED
203+
test_marks.py::check_05_TestCase_Tests::test_method_05_1 Test 2 > 0 (2 not used here) PASSED
211204
'''

0 commit comments

Comments
 (0)