From 784895a0b7ec4df3e2e99debdb4a6e73ad7c5834 Mon Sep 17 00:00:00 2001 From: medusa Date: Thu, 28 Mar 2024 19:03:21 +0000 Subject: [PATCH] Add docs/tech_docs/python/pytest.md --- docs/tech_docs/python/pytest.md | 100 ++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 docs/tech_docs/python/pytest.md diff --git a/docs/tech_docs/python/pytest.md b/docs/tech_docs/python/pytest.md new file mode 100644 index 0000000..39b69e1 --- /dev/null +++ b/docs/tech_docs/python/pytest.md @@ -0,0 +1,100 @@ +`pytest` is an essential Python library for developers focused on testing their code. It provides a powerful yet simple framework for writing and running tests, supporting unit tests, functional tests, and more complex test scenarios. `pytest` stands out for its ease of use, detailed assertions, fixtures for setup and teardown, and support for parameterized testing, among other features. Here's a concise reference guide for common use cases with `pytest`: + + +# `pytest` Reference Guide + +## Installation +``` +pip install pytest +``` + +## Basic Usage + +### Writing Your First Test +Tests are Python functions that start with `test_`. Place your test functions in files named `test_*.py` or `*_test.py`. + +```python +# content of test_sample.py +def func(x): + return x + 1 + +def test_answer(): + assert func(3) == 4 +``` + +### Running Tests +Navigate to the directory containing your test file and run: +``` +pytest +``` +`pytest` will discover and run all tests following its naming conventions. + +## Assertions +`pytest` uses standard Python `assert` for verifying expectations and values in tests, providing detailed output on assertion failures. + +```python +def test_check_equal(): + assert func(4) == 5 +``` + +## Fixtures +Fixtures provide a powerful mechanism for setup and teardown operations, creating test data, and configuring the test environment. + +```python +import pytest + +@pytest.fixture +def input_value(): + return 38 + +def test_divisible_by_2(input_value): + assert input_value % 2 == 0 +``` + +## Parameterized Tests +Parameterize tests to run the same test function with different inputs. + +```python +@pytest.mark.parametrize("test_input,expected", [(1, 2), (2, 3), (3, 4)]) +def test_eval(test_input, expected): + assert func(test_input) == expected +``` + +## Handling Exceptions +Test that a certain exception is raised using the `pytest.raises` context manager. + +```python +def test_exception(): + with pytest.raises(ValueError): + raise ValueError +``` + +## Skipping Tests +Skip a test function or expect it to fail. + +```python +@pytest.mark.skip(reason="not implemented yet") +def test_to_skip(): + pass + +@pytest.mark.xfail +def test_might_fail(): + pass +``` + +## Using Plugins and Integrations +`pytest` supports numerous plugins for integration with other tools and extending its functionality, such as `pytest-django` for Django projects, `pytest-asyncio` for testing asyncio code, and many more. + +## Configuration +Use a `pytest.ini`, `tox.ini`, or `pyproject.toml` file to configure `pytest` behavior, like custom markers, test paths, and plugin configurations. + +## Advanced Usage +`pytest` offers a broad spectrum of advanced features, including but not limited to test fixtures scopes, using mocks, and integrating with continuous integration (CI) tools. + +## Continuous Integration +`pytest` can easily integrate with CI tools like Jenkins, GitHub Actions, or GitLab CI, running your test suite on every push or pull request automatically. + +`pytest` simplifies and accelerates the testing process, encouraging best practices in test writing and execution. Its extensive documentation and active community make it a go-to choice for Python developers looking to ensure their code meets the highest standards of quality. + + +`pytest` is favored in the Python community for its rich ecosystem, simplicity, and ability to handle not just simple unit tests but also complex functional testing for applications and libraries. \ No newline at end of file