diff --git a/python-testing/.github/workflows/tests.yml b/python-testing/.github/workflows/tests.yml new file mode 100644 index 0000000000..e9b895dcb7 --- /dev/null +++ b/python-testing/.github/workflows/tests.yml @@ -0,0 +1,17 @@ +name: Tests + +on: [push, pull_request] + +jobs: + test: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.13", "3.14"] + steps: + - uses: actions/checkout@v7 + - uses: actions/setup-python@v7 + with: + python-version: ${{ matrix.python-version }} + - run: python -m pip install -r requirements.txt + - run: python -m unittest discover diff --git a/python-testing/README.md b/python-testing/README.md new file mode 100644 index 0000000000..fd97de71b1 --- /dev/null +++ b/python-testing/README.md @@ -0,0 +1,43 @@ +# Getting Started With Testing in Python + +This folder contains the sample code for the Real Python tutorial [Getting Started With Testing in Python](https://realpython.com/python-testing/). + +## Setup + +Create and activate a virtual environment, then install the pinned tools used in the tutorial: + +```sh +$ python -m venv venv/ +$ source venv/bin/activate +(venv) $ python -m pip install -r requirements.txt +``` + +The tutorial was tested with Python 3.14. The `unittest` examples only need the standard library, while `black` and `bandit` need Python 3.10 or later, and `tox` and `flake8` need Python 3.11 or later. + +## Usage + +The files follow the order of the tutorial: + +- `test_sum.py` and `test_sum_2.py`: plain `assert` scripts (`test_sum_2.py` fails on purpose) +- `test_sum_unittest.py`: the same checks as a `unittest` test case (`test_sum_tuple()` fails on purpose) +- `test_sum_pytest.py`: the same checks as `pytest` test functions +- `my_sum/`: the package under test +- `test.py`: the final test case for `my_sum.sum()`, including the intentionally failing `Fraction` test and the `.assertRaises()` test +- `tox.ini`: a Tox configuration that runs `python -m unittest discover` on Python 3.13 and 3.14 +- `.github/workflows/tests.yml`: the GitHub Actions workflow. Copy it to the `.github/workflows/` folder at the root of your own repository to use it. +- `test_bench.py` and `timeit_example.py`: the benchmarking examples + +Run the tests with any of the runners from the tutorial: + +```sh +(venv) $ python test_sum.py +(venv) $ python -m unittest -v test +(venv) $ python -m nose2 test_sum_unittest +(venv) $ python -m pytest test_sum_pytest.py +(venv) $ python -m pytest test_bench.py +(venv) $ flake8 test.py +(venv) $ bandit -r my_sum +(venv) $ tox +``` + +Several tests fail on purpose so that you can read the failure output, as the tutorial explains. The files are formatted with the repository's code style, so `black` and `flake8` may report less than the tutorial shows for its unformatted `test.py`. diff --git a/python-testing/my_sum/__init__.py b/python-testing/my_sum/__init__.py new file mode 100644 index 0000000000..9d82e37135 --- /dev/null +++ b/python-testing/my_sum/__init__.py @@ -0,0 +1,5 @@ +def sum(arg): + total = 0 + for val in arg: + total += val + return total diff --git a/python-testing/requirements.txt b/python-testing/requirements.txt new file mode 100644 index 0000000000..bdc4594f07 --- /dev/null +++ b/python-testing/requirements.txt @@ -0,0 +1,7 @@ +bandit==1.9.4 +black==26.10.1 +flake8==7.5.0 +nose2==0.16.0 +pytest==9.2.0 +pytest-benchmark==5.3.1 +tox==4.67.19 diff --git a/python-testing/test.py b/python-testing/test.py new file mode 100644 index 0000000000..cbe490eff7 --- /dev/null +++ b/python-testing/test.py @@ -0,0 +1,31 @@ +import unittest +from fractions import Fraction + +from my_sum import sum + + +class TestSum(unittest.TestCase): + def test_list_int(self): + """ + Test that it can sum a list of integers + """ + data = [1, 2, 3] + result = sum(data) + self.assertEqual(result, 6) + + def test_list_fraction(self): + """ + Test that it can sum a list of fractions + """ + data = [Fraction(1, 4), Fraction(1, 4), Fraction(2, 5)] + result = sum(data) + self.assertEqual(result, 1) + + def test_bad_type(self): + data = "banana" + with self.assertRaises(TypeError): + result = sum(data) # noqa: F841 + + +if __name__ == "__main__": + unittest.main() diff --git a/python-testing/test_bench.py b/python-testing/test_bench.py new file mode 100644 index 0000000000..05151b3a35 --- /dev/null +++ b/python-testing/test_bench.py @@ -0,0 +1,6 @@ +def my_function(): + return sum(range(100)) + + +def test_my_function(benchmark): + result = benchmark(my_function) # noqa: F841 diff --git a/python-testing/test_sum.py b/python-testing/test_sum.py new file mode 100644 index 0000000000..25b4c847a2 --- /dev/null +++ b/python-testing/test_sum.py @@ -0,0 +1,7 @@ +def test_sum(): + assert sum([1, 2, 3]) == 6, "Should be 6" + + +if __name__ == "__main__": + test_sum() + print("Everything passed") diff --git a/python-testing/test_sum_2.py b/python-testing/test_sum_2.py new file mode 100644 index 0000000000..908141dbbd --- /dev/null +++ b/python-testing/test_sum_2.py @@ -0,0 +1,12 @@ +def test_sum(): + assert sum([1, 2, 3]) == 6, "Should be 6" + + +def test_sum_tuple(): + assert sum((1, 2, 2)) == 6, "Should be 6" + + +if __name__ == "__main__": + test_sum() + test_sum_tuple() + print("Everything passed") diff --git a/python-testing/test_sum_pytest.py b/python-testing/test_sum_pytest.py new file mode 100644 index 0000000000..6d71b0a03e --- /dev/null +++ b/python-testing/test_sum_pytest.py @@ -0,0 +1,6 @@ +def test_sum(): + assert sum([1, 2, 3]) == 6, "Should be 6" + + +def test_sum_tuple(): + assert sum((1, 2, 2)) == 6, "Should be 6" diff --git a/python-testing/test_sum_unittest.py b/python-testing/test_sum_unittest.py new file mode 100644 index 0000000000..894f990526 --- /dev/null +++ b/python-testing/test_sum_unittest.py @@ -0,0 +1,13 @@ +import unittest + + +class TestSum(unittest.TestCase): + def test_sum(self): + self.assertEqual(sum([1, 2, 3]), 6, "Should be 6") + + def test_sum_tuple(self): + self.assertEqual(sum((1, 2, 2)), 6, "Should be 6") + + +if __name__ == "__main__": + unittest.main() diff --git a/python-testing/timeit_example.py b/python-testing/timeit_example.py new file mode 100644 index 0000000000..b0d4b1ffeb --- /dev/null +++ b/python-testing/timeit_example.py @@ -0,0 +1,14 @@ +def my_function(): + return sum(range(100)) + + +if __name__ == "__main__": + import timeit + + print( + timeit.timeit( + "my_function()", + setup="from __main__ import my_function", + number=100, + ) + ) diff --git a/python-testing/tox.ini b/python-testing/tox.ini new file mode 100644 index 0000000000..62fb177c05 --- /dev/null +++ b/python-testing/tox.ini @@ -0,0 +1,8 @@ +[tox] +envlist = py313, py314 + +[testenv] +deps = + +commands = + python -m unittest discover