After reading this article, the article will help readers achieve the following three goals.
OS: macOS Mojava 10.14.6 Python version: 3.7.1
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!
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.
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.
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.
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.
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
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
!
The test passed, so it was displayed as OK. You did it.
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 **.
-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.
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