Someone who can do a little operation on a black screen The specs can be anything as long as Python works
Unit test(Unit test)What is|Software validation type|Techmatrix Corporation
It refers to testing whether a program is functioning properly with the smallest particle size that makes up the program, such as the methods of the program. In the IT industry, it is also called unit testing.
There are libraries that do unit tests in each language. Below is an example
As an aside, in Ruby etc., Minitest and Rspec etc. have a unit test mechanism.
To execute Unit Test in Python, write a Py file to write the test code.
Please refer to the official Japanese documentation for writing detailed tests.
unittest — unit test framework — Python 3.8.6 documentation
Create the following files using VSCode etc.
In this case, we have listed a test case that intentionally fails.
sample.py
import unittest
class TestStringMethods(unittest.TestCase):
def test_false(self):
self.assertFalse("hoge", "hoge")
if __name__ == '__main__':
unittest.main()
Next, try running the program on the console.
When executing on the CLI, you can execute UnitTest with the following command.
python3 -m unittest sample.py
======================================================================
FAIL: test_false (test.sample.TestStringMethods)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/pcuser/Documents/GitHub/pysecret/test/sample.py", line 6, in test_false
self.assertFalse("hoge", "hoge")
AssertionError: 'hoge' is not false : hoge
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (failures=1)
If FAILD is displayed and it fails like this, it's OK If ERROR is displayed, it may be a grammatical problem in Python, so please google it.
Some common errors in Python are: [Enter a reference error message]
After this, we will actually describe the test cases.
In order to make the test as practical as possible, create a program to be tested.
hello.py
def helloMethod(str):
msg = str
return msg
if __name__ == '__main__':
main()
Then add ʻinport hello` to test.py so that helloMethod can be called.
import unittest
import hello <-Like this
This time I created a method to do a simple Echolalia. What you pass as input is the str variable of the string you pass in the method argument. And the expected value is that str will be returned as output when the method is executed.
import unittest
import hello
class TestStringMethods(unittest.TestCase):
# def test_false(self):
# self.assertFalse("hoge", "hoge")
def test_hello(self):
input_str = "Hello"
msg = hello.helloMethod(input_str)
self.assertEqual(msg, input_str)
if __name__ == '__main__':
unittest.main()
To verify that the result is correct, use the Assert method with the value stored in the msg variable. Make sure that the values of input_str are the same.
As a way of writing, since the Assert method of the UnitTest class is used, use self.assertEqual
.
Since it has already been written on the source, execute the same command that confirmed the start of UnitTest itself again.
python3 -m unittest sample.py
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
If you see the word OK on the console, the test is successful.
Recommended Posts