|
2 | 2 |
|
3 | 3 | # Unit Tests Practice |
4 | 4 |
|
5 | | -#### This repository explores some possibilities of automated tests (unittest and pytest) to verify the import and conversion of simple JSON quizzes. It was also my first steps in using Markdown documentation. |
| 5 | +#### This repo explores a wide range of testing techniques — mocking, fixtures, test-class inheritance, a simulated HTTP server and continuous integration — using simple multiple-choice quizzes stored as JSON. It also compares the automated tests through unittest and pytest, and was also my first steps with Markdown documentation. |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +| Program | Role | |
| 10 | +|---|---| |
| 11 | +| `questionnaire.py` | Loads a JSON quiz, displays it in the terminal and counts the score. | |
| 12 | +| `questionnaire_import.py` | Downloads quizzes from an online source and converts them to the project [json format](./questionnaire.md#expected-json-schema). | |
| 13 | +| `questionnaire_unit_test.py` | Automated test suite for `questionnaire.py`. | |
| 14 | + |
| 15 | +The test suite is the heart of the project: it stacks several techniques on a single use case. |
6 | 16 |
|
7 | 17 | --- |
8 | 18 |
|
9 | | -## questionnaire_import.py |
10 | | ->python3 ./questionnaire_import.py |
| 19 | +## Installation |
| 20 | + |
| 21 | +```bash |
| 22 | +python -m venv venv |
| 23 | +source venv/bin/activate # Linux / macOS (venv\Scripts\activate on Windows) |
| 24 | +pip install -r requirements.txt # to use questionnaire.py only |
| 25 | +pip install -r requirements-dev.txt # to use questionnaire_unit_tests.py |
| 26 | +``` |
11 | 27 |
|
12 | | -Loads some quizzes from a sample list in the program and converts them into a specific [json format](./questionnaire.md#expected-json-schema) before to save them |
13 | | -into the [json_questionnaires](./json_questionnaires) folder |
| 28 | +## questionnaire_import.py : Import and convert online quizzes into json_questionnaires |
| 29 | +>python3 ./questionnaire_import.py |
14 | 30 |
|
15 | 31 | More info : [questionnaire_import.md](./questionnaire_import.md) |
16 | 32 |
|
17 | 33 | --- |
18 | 34 |
|
19 | | -## questionnaire.py |
20 | | ->python3 ./questionnaire.py <questionnaire.json> |
21 | | -
|
22 | | -Displays the quiz in argument and counts the number of good answers |
| 35 | +## questionnaire.py : Displays the quiz in argument and counts the number of good answers |
| 36 | +>python3 ./questionnaire.py ./json_questionnaires/<questionnaire.json> |
23 | 37 |
|
24 | 38 | More info : [questionnaire.md](./questionnaire.md) |
25 | 39 |
|
26 | 40 | --- |
27 | 41 |
|
28 | | -## questionnaire_unit_tests.py |
29 | | ->python3 ./questionnaire_unit_tests.py |
30 | | -
|
31 | | -Uses the `unittest` automated framework but not only to test questionnaire.py |
| 42 | +## questionnaire_unit_tests.py : Run the tests with `unittest` or `pytest` |
| 43 | +# use ./json_test_files folder to check 25 intentionally broken quizzes targeting error cases |
| 44 | +>python3 -m unittest questionnaire_unit_test.py ./json_test_files/<questionnaire.json> |
| 45 | +>pytest -v questionnaire_unit_test.py -s ./json_test_files/<questionnaire.json> |
32 | 46 |
|
33 | 47 | More info : [questionnaire_unit_test.md](./questionnaire_unit_test.md) |
34 | 48 |
|
35 | | -Uses the files: |
36 | | -- json_00_empty.json |
37 | | -- json_00_without_extension |
38 | | -- json_01_good_format.json |
39 | | -- json_01_another_good_format.json |
40 | | -- pytest.ini |
41 | 49 |
|
42 | | -and files into [json_test_files](./json_test_files) folder |
| 50 | +## Testing highlights |
| 51 | + |
| 52 | +- **Test-class inheritance** to reuse base cases by changing a single parameter. |
| 53 | +- **Mocking** with `unittest.mock.patch` to simulate `sys.argv`. |
| 54 | +- **Threaded local HTTP server** (`ThreadingMixIn` + `SimpleHTTPRequestHandler`) to test |
| 55 | + remote loading without relying on the internet. |
| 56 | +- **`setUp`/`tearDown` (unittest) vs `@pytest.fixture` (pytest)** for the same need, to |
| 57 | + compare both frameworks directly. |
| 58 | + |
| 59 | + |
| 60 | +## Continuous integration |
| 61 | + |
| 62 | +GitHub Actions runs on every push and pull request to `master`: dependency install |
| 63 | +(Python 3.10), `flake8` lint, then the test suite under both `unittest` and `pytest`. |
43 | 64 |
|
44 | 65 | --- |
45 | 66 |
|
46 | 67 | ## [Requirements](./requirements.txt) |
| 68 | +- Python 3.10+ |
47 | 69 | - jsonschema==4.17.0 |
48 | 70 | - parameterized==0.8.1 |
49 | 71 | - requests==2.28.1 |
50 | 72 | - validators==0.20.0 |
51 | 73 |
|
| 74 | +## [Requirements-dev](./requirements-dev.txt) |
| 75 | +- pytest==7.2.0 |
| 76 | +- flake8==6.0.0 |
| 77 | + |
| 78 | + |
0 commit comments