diff --git a/module5.ipynb b/module5.ipynb index 1d63e67..a1758de 100644 --- a/module5.ipynb +++ b/module5.ipynb @@ -172,6 +172,48 @@ " # Assert\n", " assert all(fruit.cubed for fruit in fruit_salad.fruit)" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Comparing unittest and pytest" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Both unittest and pytest provide developers with tools to create robust and reliable code through different forms of tests. Both can be used while creating programs within Python, and it is the developer’s preference on which type they want to use.\n", + "\n", + "In this reading, you will learn about the differences between unittest and pytest, and when to use them.\n", + "\n", + "**Key differences**\n", + "\n", + "Unittest is a tool that is built directly into Python, while pytest must be imported from outside your script. Test discovery acts differently for each test type. Unittest has the functionality to automatically detect test cases within an application, but it must be called from the command line. Pytests are performed automatically using the prefix test_. Unittests use an object-oriented approach to write tests, while pytests use a functional approach. Pytests use built-in assert statements, making tests easier to read and write. On the other hand, unittests provide special assert methods like assertEqual() or assertTrue().\n", + "\n", + "Backward compatibility exists between unittest and pytest. Because unittest is built directly into Python, these test suites are more easily executed. But that doesn’t mean that pytest cannot be executed. Because of backward compatibility, the unittest framework can be seamlessly executed using the pytest framework without major modifications. This allows developers to adopt pytest gradually and integrate them into their code.\n", + "\n", + "**Key takeaways**\n", + "\n", + "Unittest and pytest are both beneficial to developers in executing tests on their code written in Python. Each one has its pros and cons, and it is up to the developer and their preference on which type of testing framework they want to use. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#!/usr/bin/env python3\n", + "import re\n", + "def rearrange_name(name):\n", + " result = re.search(r\"^([\\w .]*), ([\\w .]*)$\", name)\n", + " return \"{} {}\".format(result[2], result[1])\n", + "from rearrange import rearrange_name\n", + "\n", + "rearrange_name(\"Lovelace, Ada\") " + ] } ], "metadata": {