If you know this much, you can write Python test code! ~ Super introductory edition ~

What if I read this article

After reading this article, the article will help readers achieve the following three goals.

  1. Understand the significance of testing source code
  2. Get a feel for how to write Python test code
  3. A single file with Python test code can be executed

Execution environment

OS: macOS Mojava 10.14.6 Python version: 3.7.1

Significance of writing test code

Tests are written ** to ensure the quality of the source code. ** **

Can you trust the source code that was passed to you when you were told that you wrote the perfect source code without any bugs? If there are people who don't make any mistakes, you may trust them. But people often make mistakes.

If the test is written to some extent, you can ** trust the quality of the source code **. Also, if you have a test, you can use it as a criterion to judge whether the source code is made correctly **.

Now let's write the test in Python!

Write a test for a method that calculates the greatest common divisor

1. Specification of the method to be tested

Consider a method that calculates the greatest common divisor for testing. The greatest common divisor in English is * Greatest Common Divisor *. Let's take the acronym and name the file gcd.py.

gcd.py


def gcd(a, b):
    #Somehow a,Calculate the greatest common divisor of b
    return #The return value is the greatest common divisor

The arguments a and b of gcd () assume that an integer is input, and return the greatest common divisor of a and b as a return value.

Now that the specifications of the function to be tested are fixed, let's write the test code.

2. This is ʻimport` to write test code!

This time, we use a module called ʻunittest` to write test code in Python. Since it is installed as standard, there is no need to install it. Let's import it immediately.

test_gcd.py


import unittest
from gcd import gcd 

Let's also import the function gcd of gcd.py to test the gcd () we are testing. Now you are ready to write your test code.

3. Create a test case

Next, we will create a test case.

A test case is a ** input and one or more definitions of the expected output from the input ** to ensure that the functionality of a module or class works properly.

First, create a class that contains test cases.

test_gcd.py


import unittest
from gcd import gcd 

class GCDTest(unittest.TestCase):
    pass

** Test cases are declared by inheriting the TestCase class of the unittest module **.

By inheriting ʻunittest.TestCase`, you will be able to use the methods required to create test cases.

The class name (test case name) tests the greatest common divisor, so I chose GCDTest.

4. Write a test

Now let's write a test for the method that calculates the greatest common divisor.

test_gcd.py


import unittest
from gcd import gcd 

class GCDTest(unittest.TestCase):
    def test_gcd(self):
        self.assertEqual(15, gcd(30, 15))
        pass

Implemented a method called test_gcd.

This time we want to implement a method that calculates the greatest common divisor of two integers, so if a = 30, b = 15 is entered, the return value must be 15.

Checking this is self.assertEqual (15, gcd (30, 15)). We have confirmed that the result of gcd (30, 15) is 15.

Given that the English word assert is a word that means something like "assert", it's easy to understand. self.assertEqual (15, gcd (30, 15)) asserts that" the result of gcd (30,15) is equal to 15. " It's easy to understand.

5. Let's run the test

Use the following command to execute the test.

.sh


python -m unittest `Created module name`

The module created this time is test_gcd.py. Specify test_gcd.py and try running it.

.sh


python -m unittest test_gcd.py

Execution result スクリーンショット 2020-02-05 15.02.08.png

It has failed, but it is natural because it has not been implemented. It shows where the tests failed, the number of tests run, the run time, and more.

Let's implement the gcd method and run it again.

gcd.py


def gcd(a, b):
    return b if a%b == 0 else gcd(b, a%b)

For implementation, we use the Euclidean algorithm. Euclidean algorithm is an algorithm that finds the greatest common divisor at high speed. I won't touch it here, so if you are interested, please check it out.

Now that you've implemented it, run python -m unittest test_gcd.py! スクリーンショット 2020-02-05 15.14.55.png

The test passed, so it was displayed as OK. You did it.

Be careful when writing tests

The only test I wrote this time was self.assertEqual (15, gcd (30, 15)). Is this enough as a test case?

The answer is no. It cannot be said that the target method is implemented correctly with only one check. The reason is that the following check items will pass the test case even with the following implementation.

gcd.py


def gcd(a, b):
    return 15

This means that the greatest common divisor is 15 for any input.

When writing a case test, ** think about what the input and output patterns are and try to cover them **.

Summary

-Tests are written to ensure the quality of the source code · Use the ʻunit test module to create Python tests -When creating a test case, inherit ʻunittest.TestCase. -A test case is a blockage between the input and the output expected from the input. -Python -m unittest module name can execute the test case of the corresponding file ・ Consider what kind of input and output patterns the test cases have, and make sure that they are fully covered.

Next article

The content of this article was written from the perspective of getting a feel for it. Therefore, there is no mention of detailed methods.

This will be covered in the next article.

Coming Soon!

Recommended Posts

If you know this much, you can write Python test code! ~ Super introductory edition ~
As you may know, Python can be written like this
If you write TinderBot in Python, she can do it
Python code that keeps tweeting "Bals" as much as you can
Write selenium test code in python
If you know Python, you can make a web application with Django
You should know if you use Python! 10 useful libraries
Write code to Unit Test a Python web app
How much do you know the basics of Python?
If you don't understand mathematical symbols, you can write a program.
If __name__ == Raise your hand, if you write the code under'__main__'
If you write go table driven test in python, it may be better to use subTest