[Python] What I did to do Unit Test

table of contents

Prerequisites

Someone who can do a little operation on a black screen The specs can be anything as long as Python works

What is Unit Test in the first place?

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.

What do you need to do to test?

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]

Can't call the method?

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

Information to be input What is the information to be output?

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()

What do I need to do to pass the test?

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

[Python] What I did to do Unit Test
What I did to save Python memory
I want to do Dunnett's test in Python
What I did when updating from Python 2.6 to 2.7
What to do with PYTHON release?
What I did to welcome the Python2 EOL with confidence
How to do portmanteau test with python
What I did with a Python array
What I was addicted to Python autorun
What I did when I wanted to make Python faster -Numba edition-
What to do to get google spreadsheet in python
python unit test template
[At Coder] What I did to reach the green rank in Python
After all, what should I use to do type comparisons in Python?
What I do when imitating embedded go in python
I want to write in Python! (2) Let's write a test
[ML Ops] I want to do multi-project with Python
What to do when "cannot import name xxx" [Python]
Write code to Unit Test a Python web app
I made a python library to do rolling rank
What I did to ssh to the VPS Ubuntu environment
What I did to get started with Linux commands
What I was addicted to when using Python tornado
I want to do something in Python when I finish
What to do when you can't bind CaboCha to Python
What I learned in Python
What to do if ʻarguments [0] .scrollIntoView ();` fails in python selenium
What I did to speed up the string search task
I want to do something like sort uniq in Python
What I was addicted to when migrating Processing users to Python
What to do when "SSL: CERTIFICATE_VERIFY_FAILED _ssl.c: 1056" appears in Python
What to do when Ubuntu crashes
[Python] How to do PCA in Python
What to do if yum breaks
I tried to touch Python (installation)
What to do with Magics install
H29.2.27 ~ 3.5 Summary of what I did
To do tail recursion with Python2
Unit test log output with python
I want to do ○○ with Pandas
What to do to get tensorflow-gpu to work
I want to debug with Python
Python | What you can do with Python
Did not change from Python 2 to 3
What I did to output application logs on GAE Flexible Environment.
A note on what you did to use Flycheck with Python
What to do if Python doesn't work on Git for Windows
What to do if you can't install pyaudio with pip #Python
What to do if you get a minus zero in Python
I wanted to do it like running an AtCoder test case.
What I was addicted to when introducing ALE to Vim for Python
What I was addicted to with json.dumps in Python base64 encoding
What to do if python says "fatal error:'stdio.h' file not found"
I want to do a full text search with elasticsearch + python
What to do if Insecure Platform Warning appears when running Python
I wanted to do something like an Elixir pipe in Python
What to do if PyAudio cannot be installed on Python 3.7, 3.8, 3.9 on Windows
What to do when you get "I can't see the site !!!!"
What to do when ModuleNotFoundError: No module named'XXX' occurs in Python
What to do when the value type is ambiguous in Python?
What should I do with the Python directory structure after all?