Skip to content

Commit ad9c444

Browse files
feat:pytest
1 parent 3e6e181 commit ad9c444

11 files changed

Lines changed: 269 additions & 0 deletions

File tree

pytest_tut/README.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
### Pytest
2+
Make it easy to test your functionality. From simple, readable tests to complex functionality tests for your application and libraries.
3+
Rmoves the error from coming into production
4+
5+
6+
Example: Your function take integers value and return a integer.
7+
8+
Without test: There will be problem in behaviour of the function. what it will return
9+
10+
With test: On small code or functions you are confident about the result.
11+
12+
Test helps to make more complex code reliable and easy to add new functionality on top of existing functionality.
13+
14+
def full_name_with_hello(first_name:str, last_name:str):
15+
return 'Hello' + first_name + ' ' + last_name
16+
17+
def test_full_name_with_hello():
18+
first_name = 'Mando'
19+
last_name = 'ai'
20+
assert 'Hello' + first_name + ' ' + last_name == full_name_with_hello(first_name, last_name)
21+
22+
23+
- All the file folder names that start or ends with 'test' will run on pytest command
24+
`pytest . `
25+
- If want to run specific tests file `pytest filename.py`
26+
- we can group the tests with tests `@pytest.mark.groupname` and we test those specific tests based on group name using `pytest -m group_name -v`
27+
28+
### Fixture
29+
Are those functions which will run before the test case to which it applied.
30+
- Can feed data from database, url to test
31+
- we can attach fixture function to the tests and it will run and return the data to the test before executing each test
32+
33+
To mark a function as fixture
34+
use `@pytest.fixture`
35+
36+
@pytest.fixture
37+
def values():
38+
return 10
39+
40+
def test_increase(values):
41+
assert 15 == increase_value(input_value)
42+
43+
44+
Can make fixture functions in the file to make them accessible accross all tests
45+
46+
#### file.py
47+
48+
import pytest
49+
@pytest.fixture
50+
def value():
51+
return 10
52+
53+
### in other tests
54+
import pytest
55+
56+
def test_increase(values):
57+
assert 15 == increase_value(input_value)
58+
59+
60+
## parameterize tests
61+
we can give parameters to run tests on many inputs
62+
63+
import pytest
64+
@ptytest.mark.parameterize("input", [10,20,30,40])
65+
def test_increase(input):
66+
assert input + 5 == increment_value(input)
67+
68+
## to skip the test
69+
We can use skip anotation
70+
@pytest.mask.skip
71+
72+
## to fail funciton
73+
we can use pytest.mark.xfail
74+
75+
76+
# To run tests in parallel
77+
use
78+
79+
pytest -n 3
80+
81+
To run 3 parallel test at a time
82+
83+
84+
85+
86+

pytest_tut/poetry.lock

Lines changed: 101 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pytest_tut/pyproject.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[tool.poetry]
2+
name = "pytest-tut"
3+
version = "0.1.0"
4+
description = ""
5+
authors = ["shubham Mandowara <shubham77mandowara@gmail.com>"]
6+
readme = "README.md"
7+
8+
[tool.poetry.dependencies]
9+
python = "^3.10"
10+
pytest = "^7.4.2"
11+
12+
13+
[build-system]
14+
requires = ["poetry-core"]
15+
build-backend = "poetry.core.masonry.api"

pytest_tut/pytest_tut/__init__.py

Whitespace-only changes.

pytest_tut/pytest_tut/functions.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
def increase_value(value:int, by_value:int=5) -> int:
3+
'''function to increase the value
4+
args:
5+
value (int): value
6+
by_value (int): to increase the value by
7+
'''
8+
return value + by_value

pytest_tut/src/__init__.py

Whitespace-only changes.

pytest_tut/src/functions.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def increate_value(value:int, by=5):
2+
"""Increments the value
3+
Args:
4+
value (int): The value to increment
5+
"""
6+
return value + 5
7+
8+

pytest_tut/test_functions copy.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
from src import functions
3+
4+
def test_main():
5+
assert 15 == functions.increate_value(10)

pytest_tut/test_functions.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from pytest_tut.functions import increase_value
2+
import pytest
3+
4+
def test_increment_value_function_simple():
5+
value = 10
6+
assert value + 5 == increase_value(value=value)
7+
assert value != increase_value(value)
8+
9+
10+
@pytest.mark.dev
11+
def test_increment_value_function_group():
12+
value = 10
13+
assert value + 5 == increase_value(value=value)
14+
assert value != increase_value(value)
15+
16+
17+
@pytest.fixture
18+
def values():
19+
return 10
20+
21+
@pytest.mark.dev
22+
def test_increment_value_function_fixture(values):
23+
assert values + 5 == increase_value(value=values)
24+
assert values != increase_value(values)
25+
26+
@pytest.mark.parametrize("value",[10, 20, 30, 40, 50])
27+
def test_increment_value_function_parameter(value):
28+
assert value + 5 == increase_value(value=value)
29+
assert value != increase_value(value)
30+
31+
@pytest.mark.skip
32+
def test_increment_value_function_skip(value):
33+
assert value + 5 == increase_value(value=value)
34+
assert value == increase_value(value)
35+
36+
37+
@pytest.mark.xfail
38+
def test_increment_value_function_fail(value):
39+
assert value + 5 == increase_value(value=value)
40+
assert value == increase_value(value)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from src import functions
2+
import pytest
3+
4+
@pytest.mark.first_version
5+
def test_functions():
6+
assert 15 == functions.increate_value(value=10)

0 commit comments

Comments
 (0)