Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions python-testing/.github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -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
43 changes: 43 additions & 0 deletions python-testing/README.md
Original file line number Diff line number Diff line change
@@ -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`.
5 changes: 5 additions & 0 deletions python-testing/my_sum/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def sum(arg):
total = 0
for val in arg:
total += val
return total
7 changes: 7 additions & 0 deletions python-testing/requirements.txt
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions python-testing/test.py
Original file line number Diff line number Diff line change
@@ -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()
6 changes: 6 additions & 0 deletions python-testing/test_bench.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def my_function():
return sum(range(100))


def test_my_function(benchmark):
result = benchmark(my_function) # noqa: F841
7 changes: 7 additions & 0 deletions python-testing/test_sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def test_sum():
assert sum([1, 2, 3]) == 6, "Should be 6"


if __name__ == "__main__":
test_sum()
print("Everything passed")
12 changes: 12 additions & 0 deletions python-testing/test_sum_2.py
Original file line number Diff line number Diff line change
@@ -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")
6 changes: 6 additions & 0 deletions python-testing/test_sum_pytest.py
Original file line number Diff line number Diff line change
@@ -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"
13 changes: 13 additions & 0 deletions python-testing/test_sum_unittest.py
Original file line number Diff line number Diff line change
@@ -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()
14 changes: 14 additions & 0 deletions python-testing/timeit_example.py
Original file line number Diff line number Diff line change
@@ -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,
)
)
8 changes: 8 additions & 0 deletions python-testing/tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[tox]
envlist = py313, py314

[testenv]
deps =

commands =
python -m unittest discover
Loading