Skip to content

Commit 16f857a

Browse files
author
Sylvain MARIE
committed
Added back some stackoverflow related tests for reference
1 parent ffc20cb commit 16f857a

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# from https://stackoverflow.com/a/51199035/7262247
2+
from pytest_cases import parametrize_with_cases
3+
4+
try: # python 3.2+
5+
from functools import lru_cache
6+
except ImportError:
7+
from functools32 import lru_cache
8+
9+
10+
read_files = set()
11+
12+
13+
@lru_cache(maxsize=3)
14+
def load_file(file_name):
15+
""" This function loads the file and returns contents"""
16+
print("loading file " + file_name)
17+
global read_files
18+
assert file_name not in read_files
19+
read_files.add(file_name)
20+
return "<dummy content for " + file_name + ">"
21+
22+
23+
def case_1():
24+
return load_file('file1')
25+
26+
27+
def case_2():
28+
return load_file('file2')
29+
30+
31+
def case_3():
32+
return load_file('file3')
33+
34+
35+
@parametrize_with_cases("pars", cases=[case_1, case_2])
36+
def test_a(pars):
37+
print('test_a', pars)
38+
39+
40+
@parametrize_with_cases("pars", cases=[case_2, case_3])
41+
def test_b(pars):
42+
print('test_b', pars)
43+
44+
45+
@parametrize_with_cases("pars", cases=[case_1, case_2, case_3])
46+
def test_c(pars):
47+
print('test_c', pars)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Authors: Sylvain MARIE <[email protected]>
2+
# + All contributors to <https://github.com/smarie/python-pytest-cases>
3+
#
4+
# License: 3-clause BSD, <https://github.com/smarie/python-pytest-cases/blob/master/LICENSE>
5+
import pytest
6+
from pytest_cases import param_fixture
7+
8+
9+
class MyTester():
10+
def __init__(self, arg=["var0", "var1"]):
11+
self.arg = arg
12+
# self.use_arg_to_init_logging_part()
13+
14+
def dothis(self):
15+
print("this")
16+
17+
def dothat(self):
18+
print("that")
19+
20+
21+
# create a single parameter fixture
22+
var = param_fixture("var", [['var1', 'var2']], ids=str)
23+
24+
25+
@pytest.fixture
26+
def tester(var):
27+
"""Create tester object"""
28+
return MyTester(var)
29+
30+
31+
class TestIt:
32+
""" Tests the answer at
33+
https://stackoverflow.com/questions/18011902/py-test-pass-a-parameter-to-a-fixture-function/55394178#55394178"""
34+
def test_tc1(self, tester):
35+
tester.dothis()
36+
assert 1
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# # Authors: Sylvain MARIE <[email protected]>
2+
# # + All contributors to <https://github.com/smarie/python-pytest-cases>
3+
# #
4+
# # License: 3-clause BSD, <https://github.com/smarie/python-pytest-cases/blob/master/LICENSE>
5+
#
6+
# # From answer at https://stackoverflow.com/a/56602597/7262247
7+
# try:
8+
# from functools import lru_cache
9+
# except ImportError:
10+
# from functools32 import lru_cache
11+
#
12+
# from pytest_cases import fixture, parametrize, parametrize_with_cases, THIS_MODULE
13+
#
14+
#
15+
# # ----- "case functions" : they could be in other modules
16+
# @parametrize(name=['1_a', '2_a', '3_a'])
17+
# def case_datasetA(name):
18+
# # here you would grab the data
19+
# return "data" + name
20+
#
21+
#
22+
# @parametrize(name=['1_b', '2_b', '3_b'])
23+
# def case_datasetB(name):
24+
# # here you would grab the data
25+
# return "data" + name
26+
# # -----
27+
#
28+
#
29+
# @lru_cache()
30+
# def setup_dataset(db):
31+
# # this is run once per db thanks to the lru_cache decorator
32+
# print("setup for %s" % db)
33+
#
34+
#
35+
# @lru_cache()
36+
# def finalize_dataset(db):
37+
# # this is run once per db thanks to the lru_cache decorator
38+
# print("teardown for %s" % db)
39+
#
40+
#
41+
# @fixture(scope="module")
42+
# @parametrize_with_cases("case_data", cases=THIS_MODULE)
43+
# def data(case_data):
44+
# setup_dataset(case_data.f)
45+
# yield case_data
46+
# finalize_dataset(case_data.f)
47+
#
48+
#
49+
# def test_data(data):
50+
# # do test
51+
# pass
52+
#
53+
#
54+
# def test_synthesis(module_results_dct):
55+
# """Use pytest-harvest to check that the list of executed tests is correct """
56+
#
57+
# assert list(module_results_dct) == ['test_data[data1_a]',
58+
# 'test_data[data2_a]',
59+
# 'test_data[data3_a]',
60+
# 'test_data[data1_b]',
61+
# 'test_data[data2_b]',
62+
# 'test_data[data3_b]']

0 commit comments

Comments
 (0)