Skip to content

Commit a180e81

Browse files
first commit
0 parents  commit a180e81

File tree

1,644 files changed

+275618
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,644 files changed

+275618
-0
lines changed

Notes.txt

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
Advanced PyTest Notes
2+
3+
# conftest.py exercise # - tested value declared in fixture across multiple tests in different functions
4+
5+
- conftest.py file makes the fixtures in it available to all tests within that folder.
6+
- in the exercise is a fixture function with name 'input_value()' that, when invoked, returns integer 10.
7+
8+
9+
# test_retval1 exercise # - using conftest.py fixture for assert test
10+
11+
- 'input_value()' fixture is used as a parameter in the test .
12+
- the return value of 'input_value()' is assigned to a variable of the same name (in 'test_func_retval()' ).
13+
- 'input_value' variable is compared for equality to the return value from invoking 'func_retval()'.
14+
- ran pytest and it PASSES, as 10==10.
15+
16+
17+
# parametrization exercises # - run tests multiple times with different inputs
18+
- two types: test parametrization and fixture parametrization.
19+
20+
I. test parametrization
21+
- used the 'pytest.mark.parametrize' decorator.
22+
- contains two arguments: str variables "numerator, operator" and a list.
23+
- first number in list will be used for numerator value.
24+
- second number will be used as the operator value.
25+
26+
- '%' modulo operator used (divides left by right, then returns the remainder).
27+
- assert statement checks if a numerator divided by 5 has a remainder of 0.
28+
29+
- 'test_divisible_by_five()' test runs four times, iterating once per item on the list.
30+
- only the third test should fail, the rest pass
31+
- ran 'pytest test_param.py -v' (-v flag for verbose output)
32+
33+
34+
II. fixture parametrization
35+
- class Numerator initializes single instance attribute, 'num_list', with default value [0,0].
36+
- fixture decorator has a parameter list: 'params = [[...]]'.
37+
- 'params' keyword required, outer data structure is a list but inside values can be other data type.
38+
39+
- fixture function 'divisible_by_two' defined and has parameter 'request'.
40+
- when this fixture function runs, pytest will run once per item on list. -- three times.
41+
- each iteration, pytest passes in request object.
42+
- pytest ensures that request object will have a param attribute with the current item from the 'params' list.
43+
- the function body is able to access and test each parameter.
44+
45+
- 'divisible_by_two' instantiates an object of the Numerator class and returns it.
46+
- object can now be accessed in a test function by using 'divisible_by_two' as a parameter.
47+
48+
- 'test_divisible_by_two' instantiates an object of the Numerator class, with different values for the 'num.list' attribute.
49+
- test runs three times, each time looping through one of the three lists.
50+
- ran 'pytest test_param.py -v' verbose flag.
51+
- as expected, two tests fail and the last one passes.
52+
53+
54+
# command-line options for pytest exercise #
55+
56+
- a list of these command-line options can be seen using - pytest -h
57+
- or online at https://docs.pytest.org/en/6.2.x/reference.html#command-line-flags
58+
59+
I. Stopping test suite after a certain number of test failures
60+
61+
- '--maxfail=num' option to stop a test suite after 'num' number of times
62+
- ran 'pytest test_nfailsuite.py -v --maxfail=1'
63+
- change the value for '--maxfail=#' (--maxfail=2 and --maxfail=3)
64+
- all tests should fail.
65+
66+
67+
II. Run multiple test in parallel -- using pytest plugin pytest-xdist
68+
- useful when test suites are very large
69+
70+
- make sure your Python testing environment is activated
71+
- python -m pip install pytest-xdist
72+
73+
- '-n' option allows to specify number of tests to run in parallel
74+
- using test_param.py file
75+
- ran 'pytest test_param.py -v -n 3'
76+
-three forked processes were spawned to run three tests in parallel
77+
78+

README.md

Lines changed: 1 addition & 0 deletions
450 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

conftest.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Learning Objective: conftest.py practice and parametrizing test fixtures
2+
3+
4+
import pytest
5+
6+
7+
@pytest.fixture
8+
def input_value():
9+
input=10
10+
return input

test_nfailsuite.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def test_first_failure():
2+
numerator = 25
3+
assert numerator % 3 == 0
4+
5+
def test_second_failure():
6+
numerator = 7
7+
assert numerator % 2 == 0
8+
9+
def test_third_failure():
10+
numerator = 10
11+
assert numerator % 23 == 0

test_param.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import pytest
2+
3+
4+
# Test Parametrization exercise
5+
6+
@pytest.mark.parametrize("numerator, output", [(25,0),(5,0),(33,0),(40,0)])
7+
def test_divisible_by_five(numerator, output):
8+
assert numerator % 5 == output
9+
10+
11+
# Fixture Parametrization exercise
12+
13+
class Numerator:
14+
def __init__(self, num_list=[0,0]):
15+
self.num_list = num_list
16+
17+
@pytest.fixture(params=[[1,2], [2,5], [6,8]])
18+
def divisible_by_two(request):
19+
return Numerator(request.param)
20+
21+
def test_divisible_by_two(divisible_by_two):
22+
for num in divisible_by_two.num_list:
23+
assert num % 2 == 0

test_retval1.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def func_retval():
2+
some_int = 10
3+
return some_int
4+
5+
def test_func_retval(input_value):
6+
assert func_retval()==input_value

0 commit comments

Comments
 (0)