Unittest in python

What is Unittest

The unittest is to try out if the function or program you create works as expected. This time, I will share a rough memorandum as well.

environment

macOS 10.14.6

Basic usage of Unittest

Below is the code for testing the tashizan function. tashizan is a function that returns the sum of two arguments.

Test.py


import unittest

def tashizan(val1,val2):
    ans = val1 + val2
    return ans

class Test(unittest.TestCase):
    
    def test_tashizan(self):
        actual = tashizan(1, 2)
        expected = 3
        self.assertEqual(actual, expected)
        
if __name__ = "__main__":
    unittest.main()

The code in Test.py is explained in order from the top.

Test.py


import unittest

def tashizan(val1,val2):
    ans = val1 + val2
    return ans

Import unittest and define the function tashizan.

Test.py


class Test(unittest.TestCase):
    
    def test_tashizan(self):
        actual = tashizan(1, 2)
        expected = 3
        self.assertEqual(actual, expected)

(1) Define the Test class and inherit the TestCase class in the ʻunit test. (2) Store the answer in the variable ʻactual. ③ Store the expected answer in the variable ʻexpected. In this case, 1 + 2 = 3, so 3 is stored in ʻexpected. ④ The ʻassertEqual method in TestCase` returns whether the two arguments are equal.

Recommended Posts

Unittest in python
Unittest and CI in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
SendKeys in Python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Python unittest module execution in vs2017
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Common mock by moto in Python Unittest
Sorted list in Python
Daily AtCoder # 36 in Python
Clustering text in Python
Daily AtCoder # 2 in Python
Implement Enigma in python
Daily AtCoder # 32 in Python
Daily AtCoder # 6 in Python
Daily AtCoder # 18 in Python
Edit fonts in Python
Singleton pattern in Python
File operations in Python
Read DXF in python
Daily AtCoder # 53 in Python
Key input in Python
Use config.ini in Python
Daily AtCoder # 33 in Python
Solve ABC168D in Python