Files
learn-python3/notebooks/beginner/testing1.ipynb
Jerry Pussinen 16a7f9c8a5 Remove todo
2018-05-21 18:41:41 +02:00

151 lines
5.0 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Testing with [pytest](https://docs.pytest.org/en/latest/) - part 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Why to write tests?\n",
"* Who wants to perform manual testing?\n",
"* When you fix a bug or add a new feature, tests are a way to verify that you did not break anything on the way\n",
"* If you have clear requirements, you can have matching test(s) for each requirement\n",
"* You don't have to be afraid of refactoring\n",
"* Tests document your implementation - they show other people use cases of your implementation\n",
"* This list is endless..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## [Test-driven development](https://en.wikipedia.org/wiki/Test-driven_development) aka TDD\n",
"In short, the basic idea of TDD is to write tests before writing the actual implementation. Maybe the most significant benefit of the approach is that the developer focuses on writing tests which match with what the program should do. Whereas if the tests are written after the actual implementation, there is a high risk for rushing tests which just show green light for the already written logic.\n",
"\n",
"Tests are first class citizens in modern, agile software development, which is why it's important to start thinking TDD early during your Python learning path. \n",
"\n",
"The workflow of TDD can be summarized as follows:\n",
"1. Add a test case(s) for the change / feature / bug fix you are going to implement\n",
"2. Run all tests and check that the new one fails\n",
"3. Implement required changes\n",
"4. Run tests and verify that all pass\n",
"5. Refactor"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Running pytest inside notebooks\n",
"These are the steps required to run pytest inside Jupyter cells."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Let's make sure pytest and ipython_pytest plugin are installed\n",
"import sys\n",
"!{sys.executable} -m pip install pytest\n",
"!{sys.executable} -m pip install ipython_pytest"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Let's load ipython_pytest extension which makes it possible to run\n",
"# pytest inside notebooks\n",
"%load_ext ipython_pytest"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Due to technical limitations of Jupyter notebooks and pytest extension, all code that runs inside tests has to be in the same cell. In other words, you can not call e.g. functions that are defined in other cells. \n",
"\n",
"In real applications you have separate directory and .py files for your tests. The above mentioned limititation is only present for writing tests in Jupyter environment."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## `pytest` test cases\n",
"Pytest test cases are actually quite similar as you have already seen in the exercises. Most of the exercises are structured like pytest test cases by dividing each exercise into three cells:\n",
"1. Setup the variables used in the test\n",
"2. Your implementation\n",
"3. Verify that your implementation does what is wanted by using assertions\n",
"\n",
"See the example test case below to see the similarities between the exercises and common structure of test cases."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%pytest\n",
"# Mention this at the top of cells which contain test(s)\n",
"\n",
"# This would be in your e.g. implementation.py\n",
"def sum_of_three_numbers(num1, num2, num3):\n",
" return num1 + num2 + num3\n",
"\n",
"\n",
"# This would be in your test_implementation.py\n",
"def test_sum_of_three_numbers():\n",
" # 1. Setup the variables used in the test\n",
" num1 = 2\n",
" num2 = 3\n",
" num3 = 5\n",
" \n",
" # 2. Call the functionality you want to test\n",
" result = sum_of_three_numbers(num1, num2, num3)\n",
" \n",
" # 3. Verify that the outcome is expected\n",
" assert result == 10"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now go ahead and change the line `assert result == 10` such that the assertion fails to see the output of a failed test."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}