Skip to content

Commit fe44b64

Browse files
author
Sylvain MARIE
committed
Added/updated tests corresponding to all latest changes
1 parent 935b872 commit fe44b64

File tree

7 files changed

+158
-30
lines changed

7 files changed

+158
-30
lines changed

pytest_cases/tests/cases/doc/cases_doc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def case_foo():
1919
return
2020

2121
@case(id="hello")
22-
def blah(self):
22+
def case_blah(self):
2323
"""a blah"""
2424
return 0, 0
2525

pytest_cases/tests/cases/doc/test_doc.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,24 @@ def test_foo_default_cases_file_synthesis(request):
3232

3333

3434
@parametrize_with_cases("a,b", cases=AUTO2)
35-
def test_foo_alternate_cases_file_and_one_marked_skip(a, b):
35+
def test_foo_alternate_cases_file_and_two_marked_skip(a, b):
3636
assert isinstance(foo(a, b), tuple)
3737

3838

39-
def test_foo_alternate_cases_file_and_one_marked_skip_synthesis(request):
40-
results_dct = get_session_synthesis_dct(request, filter=test_foo_alternate_cases_file_and_one_marked_skip,
39+
def test_foo_alternate_cases_file_and_two_marked_skip_synthesis(request):
40+
results_dct = get_session_synthesis_dct(request, filter=test_foo_alternate_cases_file_and_two_marked_skip,
4141
test_id_format='function')
4242
if has_pytest_param:
4343
assert list(results_dct) == [
44-
'test_foo_alternate_cases_file_and_one_marked_skip[hello]',
45-
'test_foo_alternate_cases_file_and_one_marked_skip[two_negative_ints0]',
46-
'test_foo_alternate_cases_file_and_one_marked_skip[two_negative_ints1]'
44+
'test_foo_alternate_cases_file_and_two_marked_skip[hello]',
45+
'test_foo_alternate_cases_file_and_two_marked_skip[two_negative_ints0]',
46+
'test_foo_alternate_cases_file_and_two_marked_skip[two_negative_ints1]'
4747
]
4848
else:
4949
assert list(results_dct) == [
50-
'test_foo_alternate_cases_file_and_one_marked_skip[1hello[0]-hello[1]]',
51-
'test_foo_alternate_cases_file_and_one_marked_skip[3two_negative_ints[0]-two_negative_ints[1]]',
52-
'test_foo_alternate_cases_file_and_one_marked_skip[5two_negative_ints[0]-two_negative_ints[1]]'
50+
'test_foo_alternate_cases_file_and_two_marked_skip[0hello[0]-hello[1]]',
51+
'test_foo_alternate_cases_file_and_two_marked_skip[2two_negative_ints[0]-two_negative_ints[1]]',
52+
'test_foo_alternate_cases_file_and_two_marked_skip[4two_negative_ints[0]-two_negative_ints[1]]'
5353
]
5454

5555

@@ -101,7 +101,7 @@ def case_foo():
101101

102102
@pytest.mark.skipif(False, reason="no")
103103
@case(id="hello world")
104-
def blah(self):
104+
def case_blah(self):
105105
"""a blah"""
106106
return 0, 0
107107

@@ -155,7 +155,7 @@ def test_foo_cls_list_synthesis(request):
155155
'test_foo_cls_list[hello world1]',
156156
'test_foo_cls_list[two_negative_ints3]',
157157
# test_doc_cases.py
158-
'test_foo_cls_list[two_positive_ints1]',
158+
'test_foo_cls_list[two_positive_ints]',
159159
'test_foo_cls_list[two_negative_ints4]'
160160
]
161161
if has_pytest_param:
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from pytest_cases import parametrize, parametrize_with_cases, fixture
2+
3+
4+
already_read = set()
5+
6+
7+
@parametrize(a=range(2))
8+
def case_dummy(a):
9+
global already_read
10+
if a in already_read:
11+
raise ValueError()
12+
else:
13+
already_read.add(a)
14+
return a
15+
16+
17+
@fixture(scope='session')
18+
@parametrize_with_cases("a", cases='.')
19+
def cached_a(a):
20+
return a
21+
22+
23+
@parametrize(d=range(2))
24+
def test_caching(cached_a, d):
25+
assert d < 2
26+
assert 0 <= cached_a <= 1
27+
28+
29+
def test_synthesis(module_results_dct):
30+
assert list(module_results_dct) == [
31+
'test_caching[dummy-a=0-d=0]',
32+
'test_caching[dummy-a=0-d=1]',
33+
'test_caching[dummy-a=1-d=0]',
34+
'test_caching[dummy-a=1-d=1]'
35+
]

pytest_cases/tests/cases/doc/test_doc_filters_n_tags.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from pytest_harvest import get_session_synthesis_dct
22

3+
from pytest_cases.common_pytest_marks import has_pytest_param
34
from pytest_cases import parametrize_with_cases, case, parametrize
45

56

@@ -16,27 +17,40 @@ def case_c():
1617
return dict(name="hi i'm not used")
1718

1819

19-
class Foo:
20-
def case_two_positive_ints(self):
21-
return 1, 2
22-
23-
@case(tags='foo')
24-
def case_one_positive_int(self):
25-
return 1
20+
def user_bob():
21+
return "bob"
2622

2723

2824
@parametrize_with_cases("data", cases='.', prefix="data_")
29-
def test_with_data(data):
25+
@parametrize_with_cases("user", cases='.', prefix="user_")
26+
def test_with_data(data, user):
3027
assert data in ('a', "hello", "world")
28+
assert user == 'bob'
3129

3230

3331
def test_with_data_synthesis(request):
3432
results_dct = get_session_synthesis_dct(request, filter=test_with_data, test_id_format='function')
35-
assert list(results_dct) == [
36-
'test_with_data[a]',
37-
'test_with_data[b-True]',
38-
'test_with_data[b-False]'
39-
]
33+
if has_pytest_param:
34+
assert list(results_dct) == [
35+
'test_with_data[bob-a]',
36+
'test_with_data[bob-b-True]',
37+
'test_with_data[bob-b-False]'
38+
]
39+
else:
40+
assert list(results_dct) == [
41+
'test_with_data[a-bob]',
42+
'test_with_data[b-True-bob]',
43+
'test_with_data[b-False-bob]'
44+
]
45+
46+
47+
class Foo:
48+
def case_two_positive_ints(self):
49+
return 1, 2
50+
51+
@case(tags='foo')
52+
def case_one_positive_int(self):
53+
return 1
4054

4155

4256
@parametrize_with_cases("a", cases=Foo, has_tag='foo')
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
from math import sqrt
2+
import pytest
3+
4+
from pytest_cases.common_pytest_marks import has_pytest_param
5+
from pytest_cases import parametrize_with_cases
6+
7+
8+
def case_int_success():
9+
return 1
10+
11+
12+
def case_negative_int_failure():
13+
# note that we decide to return the expected type of failure to check it
14+
return -1, ValueError, "math domain error"
15+
16+
17+
@parametrize_with_cases("data", cases='.', glob="*success")
18+
def test_good_datasets(data):
19+
assert sqrt(data) > 0
20+
21+
22+
@parametrize_with_cases("data, err_type, err_msg", cases='.', glob="*failure")
23+
def test_bad_datasets(data, err_type, err_msg):
24+
with pytest.raises(err_type, match=err_msg):
25+
sqrt(data)
26+
27+
28+
def test_synthesis(module_results_dct):
29+
if has_pytest_param:
30+
assert list(module_results_dct) == [
31+
'test_good_datasets[int_success]',
32+
'test_bad_datasets[negative_int_failure]'
33+
]
34+
else:
35+
assert list(module_results_dct) == [
36+
'test_good_datasets[int_success]',
37+
'test_bad_datasets[negative_int_failure[0]-negative_int_failure[1]-negative_int_failure[2]]'
38+
]
39+
40+
41+
def create_filter(sub_str):
42+
def my_filter(case_func):
43+
return sub_str in case_func._pytestcase.id
44+
return my_filter
45+
46+
47+
@parametrize_with_cases("data", cases='.', filter=lambda case_func: "success" in case_func._pytestcase.id)
48+
def test_good_datasets2(data):
49+
assert sqrt(data) > 0
50+
51+
52+
@parametrize_with_cases("data, err_type, err_msg", cases='.', filter=create_filter("failure"))
53+
def test_bad_datasets2(data, err_type, err_msg):
54+
with pytest.raises(err_type, match=err_msg):
55+
sqrt(data)
56+
57+
58+
def test_synthesis2(module_results_dct):
59+
if has_pytest_param:
60+
assert list(module_results_dct) == [
61+
'test_good_datasets[int_success]',
62+
'test_bad_datasets[negative_int_failure]',
63+
'test_synthesis',
64+
'test_good_datasets2[int_success]',
65+
'test_bad_datasets2[negative_int_failure]'
66+
]
67+
else:
68+
assert list(module_results_dct) == [
69+
'test_good_datasets[int_success]',
70+
'test_bad_datasets[negative_int_failure[0]-negative_int_failure[1]-negative_int_failure[2]]',
71+
'test_synthesis',
72+
'test_good_datasets2[int_success]',
73+
'test_bad_datasets2[negative_int_failure[0]-negative_int_failure[1]-negative_int_failure[2]]'
74+
]

pytest_cases/tests/cases/doc/test_fixtures.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def test_users(a, db, request):
2626
def test_users_synthesis(request, db):
2727
results_dct = get_session_synthesis_dct(request, filter=test_users, test_id_format='function')
2828
assert list(results_dct) == [
29-
'test_foo_fixtures[a_is_bob]',
30-
'test_foo_fixtures[a_is_from_db-id=0]',
31-
'test_foo_fixtures[a_is_from_db-id=1]'
29+
'test_users[a_is_bob]',
30+
'test_users[a_is_from_db-id=0]',
31+
'test_users[a_is_from_db-id=1]'
3232
]

pytest_cases/tests/cases/legacy/intermediate/test_filtering.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ def case_a():
2323
return ins, outs, None
2424

2525

26-
@cases_data(module=THIS_MODULE, filter=lambda tags: 'a' in tags or 'b' in tags)
26+
def my_tag_filter(case_func):
27+
tags = case_func._pytestcase.tags
28+
return 'a' in tags or 'b' in tags
29+
30+
31+
@cases_data(module=THIS_MODULE, filter=my_tag_filter)
2732
def test_with_cases_a_or_b(case_data # type: CaseDataGetter
2833
):
2934

@@ -36,7 +41,7 @@ def test_with_cases_a_or_b(case_data # type: CaseDataGetter
3641
assert outs == expected_o
3742

3843

39-
@cases_data(module=THIS_MODULE, filter=lambda tags: 'a' in tags and 'b' in tags)
44+
@cases_data(module=THIS_MODULE, filter=my_tag_filter)
4045
def test_with_cases_a_and_b(case_data # type: CaseDataGetter
4146
):
4247

0 commit comments

Comments
 (0)