Skip to content

Commit fdc7361

Browse files
committed
pytest override a fixture with direct test parametrization
1 parent e814bcd commit fdc7361

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

case/pytest/conftest.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,27 @@ def cleandir():
4040
# 1/0
4141
# with pytest.raises(ZeroDivisionError):
4242
# 1 / 0
43+
44+
45+
@pytest.fixture
46+
def username():
47+
''' username '''
48+
return 'username'
49+
50+
51+
@pytest.fixture
52+
def other_username(username):
53+
''' other_username '''
54+
return 'other-' + username
55+
56+
57+
@pytest.fixture(params=['one', 'two', 'three'])
58+
def parametrized_username(request):
59+
''' parametrized_username '''
60+
return request.param
61+
62+
63+
@pytest.fixture
64+
def non_parametrized_username(request):
65+
''' non_parametrized_username '''
66+
return 'username'

case/pytest/test_something.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# -*- coding:utf-8 -*-
2+
3+
import os
4+
import pytest
5+
6+
7+
def test_username_0(username):
8+
''' test_username_0 '''
9+
assert username == 'username'
10+
11+
12+
@pytest.mark.parametrize('username', ['directly-overridden-username'])
13+
def test_username_1(username):
14+
''' test_username_1 '''
15+
assert username == 'directly-overridden-username'
16+
17+
18+
@pytest.mark.parametrize('username', ['directly-overridden-username-other-repaired'])
19+
def test_username_other(username, other_username):
20+
''' test_username_other '''
21+
print('\n\n test_username_other::username: {}'.format(username))
22+
print(' test_username_other::other_username: {}'.format(other_username))
23+
assert username == 'directly-overridden-username-other-repaired'
24+
assert other_username == 'other-directly-overridden-username-other-repaired'
25+
26+
27+
@pytest.fixture
28+
def parametrized_username():
29+
''' parametrized_username '''
30+
return 'overridden-username'
31+
32+
33+
@pytest.fixture(params=['one', 'two', 'three'])
34+
def non_parametrized_username(request):
35+
''' non_parametrized_username '''
36+
return request.param
37+
38+
39+
def test_parametrized_username_0(parametrized_username):
40+
''' test_parametrized_username_0 '''
41+
assert parametrized_username == 'overridden-username'
42+
43+
44+
def test_parametrized_username_1(non_parametrized_username):
45+
''' test_parametrized_username_1 '''
46+
assert non_parametrized_username in ['one', 'two', 'three']
47+
48+
49+
if __name__ == "__main__":
50+
''' the main point '''
51+
retcode = pytest.main(args=['-v', '-s', os.path.abspath(__file__)])
52+
#
53+
# retcode = pytest.main(args=['-v', '-s', '{}::{}'.format(os.path.abspath(__file__), 'test_username_0')])
54+
# retcode = pytest.main(args=['-v', '-s', '{}::{}'.format(os.path.abspath(__file__), 'test_username_1')])
55+
# retcode = pytest.main(args=['-v', '-s', '{}::{}'.format(os.path.abspath(__file__), 'test_username_other')])
56+
# retcode = pytest.main(args=['-v', '-s', '{}::{}'.format(os.path.abspath(__file__), 'test_parametrized_username_0')])
57+
# retcode = pytest.main(args=['-v', '-s', '{}::{}'.format(os.path.abspath(__file__), 'test_parametrized_username_1')])
58+
#
59+
# retcode = pytest.main(args=['--fixtures', os.path.abspath(__file__)])
60+
# retcode = pytest.main(args=['--collect-only', os.path.abspath(__file__)])
61+
print(retcode)

case/pytest/test_something_else.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# -*- coding:utf-8 -*-
2+
3+
import os
4+
import pytest
5+
6+
7+
def test_parametrized_username_0(parametrized_username):
8+
''' test_parametrized_username_0 '''
9+
assert parametrized_username in ['one', 'two', 'three']
10+
11+
12+
def test_parametrized_username_1(non_parametrized_username):
13+
''' test_parametrized_username_1 '''
14+
assert non_parametrized_username == 'username'
15+
16+
17+
if __name__ == "__main__":
18+
''' the main point '''
19+
retcode = pytest.main(args=['-v', '-s', os.path.abspath(__file__)])
20+
#
21+
# retcode = pytest.main(args=['-v', '-s', '{}::{}'.format(os.path.abspath(__file__), 'test_parametrized_username_0')])
22+
# retcode = pytest.main(args=['-v', '-s', '{}::{}'.format(os.path.abspath(__file__), 'test_parametrized_username_1')])
23+
#
24+
# retcode = pytest.main(args=['--fixtures', os.path.abspath(__file__)])
25+
# retcode = pytest.main(args=['--collect-only', os.path.abspath(__file__)])
26+
print(retcode)

0 commit comments

Comments
 (0)