Meeting the Unittest
1. Meeting the Unittest
Today we will learn about the unittest framework.2. Recap of OOP
Before diving into the unittest, let's recap the basics of Object Oriented Programming or OOP. OOP is a programming paradigm based on the idea of classes and objects. Class is a blueprint of an object that can contain functions called methods and variables called attributes. Finally, an object is an instance of a class. Let's say we want to create a class that represents an abstract rectangle with two sides and a method to calculate its area. To do this, we declared the "Rectangle" class with the two methods. The "Init" method is always executed on the object's initialization. In this case, it gets the rectangle sides and saves it to the object. The "get area" method returns the rectangle area.3. OOP Inheritance
Classes can inherit properties from other classes. For example, we have the "Rectangle" class and want to create the "RedRectangle" class. We do not have to create the "RedRectangle" from scratch. Instead, we inherit all the "Rectangle" properties and change the color to red.4. What is unittest
Let's get to the unittest. Unittest is a built-in Python framework for test automation. Depsite of its name, it is not only purposed for unit tests. We can implement all kinds of tests with it! It is based on OOP and uses such testing concepts as fixtures, test cases, and test suites, which are, in fact, very similar to what we already know. A test case is an instance of testing. A test suite is a collection of test cases and/or test suites.5. unittest vs. pytest
Here are some differences between the two testing frameworks. Unittest is OOP-based, and it requires you to create a test class. Pytest is function-based, so you don't have to use classes. Unittest is a Python built-in library, and pytest is a third-party package. Unittest has also more assertion methods.6. How to create a test with unittest
Let's find out how to create a simple unittest test case. Here, we want to test that the exponentiation operator works as expected by checking that minus three squared equals nine. To do this, we imported the unittest library. We then declared the TestSquared class. Note TestSquared must inherit from unittest dot TestCase. Then, we define the test function - "test negative". To check something in unittest, we can use the assertion methods very similar to pytest. The main difference is that we call a method from the TestCase class instead of Python "assert".7. Assertion methods
Now we will dive into the unittest assertion methods. We already saw the "assertEqual" method, which lets us check whether some "A" is equal to some "B". And its inverse method - "assertNotEqual". To check the truthfulness of some expression in unittest, we use "assertTrue" and "assertFalse". We can check that objects are equal with "assertIs", and that an object is "None" with "assertIsNone". There is also "assertIsInstance" to check the object's type and "assertIn" to verify that the object is present in a container (for example, a list). Finally, there is "assertRaise" to check that the given exception is raised when the function is called. There are many other methods that we don't cover in this course, but one can find them in the unittest documentation.8. Summary
We learned the basics of unittest. Unittest is an OOP-based test automation framework. An instance of testing is called "TestCase". To create a test, we have to declare a class inheriting from unittest-dot-TestCase. We can also use a wide variety of built-in assertion methods.9. Let's practice!
Now you will practice on creating awesome tests with the unittest framework.Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.