This project provides resources and examples for various software best concepts and practices. It aims to be a valuable learning tool for developers and testers.
- Pytest is a popular Python testing framework known for its flexibility, expressiveness, and ease of use. It promotes writing clean, readable tests with features like fixtures, parametrization, and assertions.
- Coverage is a Python library that calculates the percentage of your code that is executed by your tests. This helps you identify areas where you might need to write more tests to achieve better code coverage.
- Linting is an automated process that analyzes your code to identify potential errors, stylistic inconsistencies, and readability issues. It's like a grammar and style checker for programmers, helping you maintain clean, consistent, and well-formatted code.
- Code formatters are tools that automatically format your code according to a set of rules or style guides. They can fix indentation, spacing, alignment, line breaks, and other aspects of code appearance.
- Pre-commit hooks are scripts that execute before you commit your code changes in a Git repository. They act as a safety net, automatically running checks and validations on your code to identify potential issues before they are integrated into the main codebase.
pip install pytest coverage pylint black pre-commit
-
Create a
test
folder -
Create a test file for each of your python file implementation with the name either starting, ending or having
test
in it. Egtest_
,_test_
,_test.py
. -
The names of your test methods should start with
test_
-
Write your unit test and integration test in all of the test files.
-
You can run your test either by running this at the root directory
pytest
Or
pytest test/test_class.py
-
Next you check for test coverage. Create
.coveragerc
filetouch .coveragerc
-
Add the following into it. Note: Use the name of the directoey containing your logic
[run] source = src
-
Run the following to generate a coverage report
coverage run -m pytest -v coverage report -m coverage html
- To run pytest use
this runs all the files with starting or ending
pytest
test_
or havetest
in them - To get pytest to print things to the commandline use -s flag
pytest test_case/test_circle.py -s
- To see each test case in each test file being run do this pytest -v
pytest -v pytest test_case/test_rectangle.py -v
- To run a particular marker do this
pytest -m slow
- To run a particular test function in a test file do this
pytest test_case/test_api.py::test_can_list_tasks
-
Install pylint extension on vscode
-
Start the pylint server by going to the command palette
-
To run pylint on terminal
pylint --recursive=y .
- Black is the uncompromising Python code formatter
black --diff . | less black test_case/test_my_function.py --diff black test_case/test_my_function.py
-
Install the pre-commit hook:
pip install pre-commit
-
Create a configuration file called
.pre-commit-config.yaml
pre-commit sample-config > .pre-commit-config.yaml
-
Run against all the files
pre-commit run --all-files
-
To automate pre-commit checks, you will have to install the git hook scripts.
pre-commit install
-
Now pre-commit checks is done after every commits
-
Now you can make changes and commit. It should return something like this: