2. TESTING
• Testing is finding out how well something works.
How do you test whether a new table you bought is strong enough?
(revealing capabilities by putting it under strain)
Now, lets look a little more into software testing…
3. WHAT ARE THE DIFFERENT TESTS?
• Unit tests
• Integration tests
• Functional tests
• Non function tests
• UAT (User Acceptance)
• Stress test
• Regression tests
And many more.. (100+ types)
**We just use the first two
4. UNIT TEST
• Why?
- To detect changes that may break a design contract
- To reduces defects in the newly developed features
- To verify the accuracy/functionality of a unit.
Two popular ways to do it in python
unittest pytest
To write better code.
• How?
By testing individual units/components of the app/software.
A unit is the smallest testable part of any software.
5. WHY PYTEST?
• Popular
• Concise
• Has helpful assert messages
• Powerful fixture
• etc .. (Don’t ask me what more!)
6. INSTALL & SETUP
• To install pytest -
pip install pytest
• unittest module is part of python3
• Coverage module –
pip install coverage
OR
pip install pytest-cov
• Run tests –
• pytest arg1 arg2 arg3
• Run coverage –
• coverage run -m pytest arg1 arg2 arg3
OR
• pytest --cov=src/ tests/
7. PYTEST – TEST DISCOVERY
• Collection starts from the cmd line args which maybe directories, files or test ids.
• Recurse into directories
• Files with *_test.py or test_*.py
• Test prefixed test classes without an init method
• test_ prefixed methods within them
8. PYTEST – FEW OPTIONS
• -r : short test summary info
and its variants,
f - failed
E - error
s - skipped
x - xfailed
X - xpassed
p - passed
P - passed with output
• Special characters for (de)selection of groups:
a - all except pP
A - all
N - none, this can be used to display nothing (since fE is the default)
• --pdb : PDB (Python Debugger) on failures
9. • setUp() - to set up initial state for all test methods
• tearDown() – to clean-up after a test method runs.
• setUpClass()
• tearDownClass()
• run() – Define the order of running the test cases
• skipTest(reason)
• subTest(msg=None, **params)
UNITTEST - Unit testing framework
Read More: https://docs.python.org/3/library/unittest.html
10. MOCKING & PATCHING
• Mock and MagicMock objects create all attributes and methods as you access
them and store details of how they have been used.
• The patch() decorator / context manager makes it easy to mock classes or objects
in a module under test.
11. FIXTURES
• pytest fixtures offer dramatic improvements over the classic xUnit style of
setup/teardown functions:
• fixtures have explicit names and are activated by declaring their use from test
functions, modules, classes or whole projects.
• fixtures are implemented in a modular manner, as each fixture name triggers a fixture
function which can itself use other fixtures.
• fixture management scales from simple unit to complex functional testing, allowing to
parametrize fixtures and tests according to configuration and component options, or
to re-use fixtures across function, class, module or whole test session scopes.