Pharmaceutical company researchers summarized Python unit tests

Introduction

Here, I will explain the Python test using ʻunit test`.

Test basics

The test is written as follows so that it corresponds to the function to be dealt with.

import unittest


def average(num1, num2):
    return (num1 + num2) / 2


class AverageTests(unittest.TestCase):
    
    def test_average(self):
        actual = average(1, 2)
        expected = 1.5
        self.assertEqual(actual, expected)

For the function ʻaverage, a class called ʻAverageTests is prepared, and test_average is written as a method in this class. As you can see, the function for testing is often test_function name to be tested.

ʻAssert` method

As in the example above, the ʻassert method is used when making test decisions. Some of the most commonly used ʻassert methods are:

--assertEqual (actual, expected): Whether actual and expected are equal --assertNotEqual (actual, expected): Whether actual and expected are not equal --assertTrue (bool): Whether bool is True --assertFalse (bool): Whether bool is False --assertGreater (num1, num2): Whether num1 is greater than num2 --assertGreaterEqual (num1, num2): Whether num1 is num2 or higher --assertLess (num1, num2): Whether num1 is less than num2 --assertLessEqual (num1, num2): Whether num1 is less than or equal to num2 --assertIn (value, values): Whether value is included in values

Summary

Here, I explained how to write a Python test using ʻunit test`. I would like to improve development efficiency by putting tests on my side.

Recommended Posts

Pharmaceutical company researchers summarized Python unit tests
Pharmaceutical company researchers summarized Python control statements
Pharmaceutical company researchers summarized classes in Python
Pharmaceutical company researchers summarized functions in Python
Pharmaceutical company researchers summarized Python exception handling
Pharmaceutical company researchers summarized Python coding standards
Pharmaceutical company researchers summarized variables in Python
Pharmaceutical company researchers summarized SciPy
Pharmaceutical company researchers summarized regular expressions in Python
Pharmaceutical company researchers summarized RDKit
Pharmaceutical company researchers summarized scikit-learn
Pharmaceutical company researchers summarized web scraping using Python
Pharmaceutical company researchers summarized Pandas
Pharmaceutical company researchers summarized file scanning in Python
Pharmaceutical company researchers summarized database operations using Python
Pharmaceutical company researchers summarized NumPy
Pharmaceutical company researchers summarized Matplotlib
Pharmaceutical company researchers summarized Seaborn
Pharmaceutical company researchers summarized Python's comprehensions
Python unit tests
Pharmaceutical company researchers have summarized the operators used in Python
Pharmaceutical company researchers summarized Python's data structures
How to install Python for pharmaceutical company researchers
Write C unit tests in Python
A pharmaceutical company researcher summarized the basic description rules of Python
Summary of Python articles by pharmaceutical company researcher Yukiya
python unit test template
Library path setting to pass GAE / Python local unit tests