Skip to content

Commit dab5e82

Browse files
committed
update MockingPatching
1 parent 767f75c commit dab5e82

File tree

2 files changed

+385
-203
lines changed

2 files changed

+385
-203
lines changed

notebooks/CodingBestPractices.ipynb

+65-33
Original file line numberDiff line numberDiff line change
@@ -944,37 +944,6 @@
944944
"test_add_mixed()"
945945
]
946946
},
947-
{
948-
"cell_type": "markdown",
949-
"metadata": {},
950-
"source": [
951-
"## Pytest\n",
952-
"\n",
953-
"- Pytest helps your write better programs: [https://docs.pytest.org/en/8.0.x/](https://docs.pytest.org/en/8.0.x/)\n",
954-
"- Must explictly install pytest; not a part of standard library\n",
955-
"- `pip install -U pytest`\n",
956-
"- pytest can be executed for a single file, or all the files and subfolders within a folder\n",
957-
"- pytest can discover all the test modules that are named `test_*.py`\n",
958-
"- pytest will execute every `test_*()` function in the `test` module"
959-
]
960-
},
961-
{
962-
"cell_type": "markdown",
963-
"metadata": {},
964-
"source": [
965-
"## Code coverage by unit tests\n",
966-
"\n",
967-
"- though not sufficient, code coverage provides measureable metrics to determine how good the test cases are\n",
968-
"- we can use `pytest-cov` plugin for pytest to demonstrate the code coverage from our tests\n",
969-
"- it must be installed `pip install pytest-cov`\n",
970-
"- more on it here: [https://pytest-cov.readthedocs.io/en/latest/readme.html](https://pytest-cov.readthedocs.io/en/latest/readme.html)\n",
971-
"- see [https://github.com/rambasnet/course-container](https://github.com/rambasnet/course-container)\n",
972-
" - uses [https://app.codecov.io/](https://app.codecov.io/) that provides code coverage report and badge if ci-cd is configured\n",
973-
" - you can login with github on the service and just configure each repo to use the app\n",
974-
" - see the ci-test.yml file: [https://github.com/rambasnet/course-container/blob/main/.github/workflows/ci-test.yml](https://github.com/rambasnet/course-container/blob/main/.github/workflows/ci-test.yml)\n",
975-
"- see and run the Makefile in the root folder of this repo from a terminal"
976-
]
977-
},
978947
{
979948
"cell_type": "markdown",
980949
"metadata": {},
@@ -1033,6 +1002,69 @@
10331002
"- Makefile is installed in Docker image"
10341003
]
10351004
},
1005+
{
1006+
"cell_type": "markdown",
1007+
"metadata": {},
1008+
"source": [
1009+
"## Pytest\n",
1010+
"\n",
1011+
"- Pytest helps your write better programs: [https://docs.pytest.org/en/8.0.x/](https://docs.pytest.org/en/8.0.x/)\n",
1012+
"- Must explictly install pytest; not a part of standard library\n",
1013+
"- `pip install -U pytest`\n",
1014+
"- pytest can be executed for a single file, or all the files and subfolders within a folder\n",
1015+
"- pytest can discover all the test modules that are named `test_*.py`\n",
1016+
"- pytest will execute every `test_*()` function in the `test` module"
1017+
]
1018+
},
1019+
{
1020+
"cell_type": "markdown",
1021+
"metadata": {},
1022+
"source": [
1023+
"## Code coverage by unit tests\n",
1024+
"\n",
1025+
"- though not sufficient, code coverage provides measureable metrics to determine how good the test cases are\n",
1026+
"- more on it here: [https://pytest-cov.readthedocs.io/en/latest/readme.html](https://pytest-cov.readthedocs.io/en/latest/readme.html)\n",
1027+
"- **code coverage** is a count of the number of lines of code that are executed by a program and by test code\n",
1028+
"- we can download and use **pytest-cov** plugin to do just that\n",
1029+
"- install coverage first; Docker image already has it installed\n",
1030+
"\n",
1031+
"```bash\n",
1032+
"pip install pytest-cov\n",
1033+
"```\n",
1034+
"\n",
1035+
"- use `#pragma: no cover` directive to tell pytest-cov not to count the function/code if you don't know how to test certain code (yet!)\n",
1036+
"- html report is created in `htmlcov` folder\n",
1037+
"- note coverage creates `.gitignore` file inside `htmlcov` that ignores all the files in the folder to be tracked\n",
1038+
"- delete the `.gitignore` file to track and html reports into your repository\n",
1039+
"\n",
1040+
"### Write more tests for 100% code coverage\n",
1041+
"\n",
1042+
"- first run coverage on test_averages.py file as it is\n",
1043+
"- then add more tests to to cover 100% code\n",
1044+
"- run pytest-cov to verify...\n",
1045+
"- see Makefile in demo-assignments\n",
1046+
"- run the Makefile in the root folder of this repo from a terminal\n",
1047+
"\n",
1048+
"```bash\n",
1049+
"(py) ╭─rbasnet@M-rbasnetMBP ~/projects/Object-Oriented-Programming-Design-Patterns/demo-assignments \n",
1050+
"╰─$ make run-test-coverage\n",
1051+
"\n",
1052+
"...\n",
1053+
"\n",
1054+
"---------- coverage: platform darwin, python 3.10.9-final-0 ----------\n",
1055+
"Name Stmts Miss Cover Missing\n",
1056+
"------------------------------------------------------------------------------------\n",
1057+
"demo-assignments/A2-ABC/egypt/egypt.py 45 0 100%\n",
1058+
"demo-assignments/A2-ABC/egypt/kattis.py 20 0 100%\n",
1059+
"demo-assignments/A2-ABC/egypt/tests/__init__.py 0 0 100%\n",
1060+
"demo-assignments/A2-ABC/egypt/tests/test_egypt.py 68 0 100%\n",
1061+
"demo-assignments/A2-ABC/egypt/tests/test_triangle.py 28 0 100%\n",
1062+
"demo-assignments/A2-ABC/egypt/triangle.py 34 0 100%\n",
1063+
"------------------------------------------------------------------------------------\n",
1064+
"TOTAL 195 0 100%\n",
1065+
"```"
1066+
]
1067+
},
10361068
{
10371069
"cell_type": "markdown",
10381070
"metadata": {},
@@ -1041,7 +1073,7 @@
10411073
],
10421074
"metadata": {
10431075
"kernelspec": {
1044-
"display_name": "Python 3",
1076+
"display_name": "Python 3 (ipykernel)",
10451077
"language": "python",
10461078
"name": "python3"
10471079
},
@@ -1055,7 +1087,7 @@
10551087
"name": "python",
10561088
"nbconvert_exporter": "python",
10571089
"pygments_lexer": "ipython3",
1058-
"version": "3.12.1"
1090+
"version": "3.10.8"
10591091
}
10601092
},
10611093
"nbformat": 4,

0 commit comments

Comments
 (0)