Exclude specified cases with Python unittest

Summary

Introduction

Even if there are multiple means of transportation to reach the destination in the real world, such as walking, car, and public transportation, the destination is one of the destinations, but how much each means can be affected by the price, time, and accident. There are advantages and disadvantages such as.

Similarly, when writing code, there can be multiple ways to achieve the same goal.

For example, when searching a tree, there are multiple search methods such as depth-first and breadth-first, which have advantages and disadvantages. The sorting algorithm is similar. It's a bit overstated, but similar cases can occur in cases like Strategy in the design pattern.

In the case of selecting depth-first in some cases and breadth-first in some cases, each test case will be placed in the code, and if you want to perform a black box test without being aware of the contents of the code, implement it honestly. Then there will be duplicate similar test code.

In such a case, I will write a story that load_tests () can be used to write code that limits test cases.

code

As an example, consider implementing the multiplication of integers x and y in two ways, "adding y x times" and "multiplication", and implementing each test.

1) Implementation and testing of addition and multiplication

A naive implementation would result in the following code. Comment The part written as "redundant" is redundant.

import unittest

class MultiplyBase(object):
    def multiply(self, n1: int, n2: int) -> int:
        raise NotImplementedError()

class MultiplyPlus(MultiplyBase):
    def multiply(self, n1: int, n2: int) -> int:
        ret = 0
        for i in range(0, n1):
            ret += n2
        return ret

class MultiplyAsterisk(MultiplyBase):
    def multiply(self, n1: int, n2: int) -> int:
        return n1 * n2

class TestMultiplyBase(unittest.TestCase):
    def test_multiply(self):
        obj: MultiplyBase = MultiplyBase()
        with self.assertRaises(NotImplementedError):
            obj.multiply(2, 3)

class TestMultiplyPlus(unittest.TestCase):
    def test_multiply(self):
        obj: MultiplyBase = MultiplyPlus()
        self.assertEqual(obj.multiply(2, 3), 6)  #Redundancy!!

class TestMultiplyAsteriskClass(unittest.TestCase):
    def test_multiply(self):
        obj: MultiplyBase = MultiplyAsterisk()
        self.assertEqual(obj.multiply(2, 3), 6)  #Redundancy!!

I don't really care about this amount, but it's hard when it's large.

2) Implementation and testing that eliminates redundant parts

We will implement a test case in the TestCaseHelper class and derive this test case to create a concrete test case.

With TestCaseHelper, each test case class only needs to implement which class of objects it uses. This eliminates redundant code.

However, in this case, the TestCaseHelper class will be discovered with the default behavior of unittest. Because of this, test cases always fail, so load_tests () prevents TestCaseHelper from being considered a test case.

import unittest

class MultiplyBase(object):
    def multiply(self, n1: int, n2: int) -> int:
        raise NotImplementedError()

class MultiplyPlus(MultiplyBase):
    def multiply(self, n1: int, n2: int) -> int:
        ret = 0
        for i in range(0, n1):
            ret += n2
        return ret

class MultiplyAsterisk(MultiplyBase):
    def multiply(self, n1: int, n2: int) -> int:
        return n1 * n2

class TestCaseHelper(unittest.TestCase):
    def _get_multiply_obj(self) -> MultiplyBase:
        raise NotImplementedError()

    def test_multiply(self):
        obj: MultiplyBase = self._get_multiply_obj()
        self.assertEqual(obj.multiply(2, 3), 6)

class TestMultiplyBase(TestCaseHelper):
    def _get_multiply_obj(self) -> MultiplyBase:
        return MultiplyBase()

    def test_multiply(self):
        obj: MultiplyBase = self._get_multiply_obj()
        with self.assertRaises(NotImplementedError):
            obj.multiply(2, 3)

class TestMultiplyPlus(TestCaseHelper):
    def _get_multiply_obj(self) -> MultiplyBase:
        return MultiplyPlus()

class TestMultiplyAsterisk(TestCaseHelper):
    def _get_multiply_obj(self) -> MultiplyBase:
        return MultiplyAsterisk()

def load_tests(loader, tests, patterns):
    test_cases = (TestMultiplyBase, TestMultiplyPlus, TestMultiplyAsterisk)
    suite = unittest.TestSuite()
    for test_class in test_cases:
        tests = loader.loadTestsFromTestCase(test_class)
        suite.addTests(tests)
    return suite

You have now successfully excluded some test cases with load_tests ().

Recommended Posts

Exclude specified cases with Python unittest
Exclude tweets containing URLs with tweepy [Python]
Use selenium phantomjs webdriver with python unittest
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Socket communication with Python
Data analysis with python 2
Try scraping with Python.
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Run Python with VBA
Handling yaml with python
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Run prepDE.py with python3
1.1 Getting Started with Python
Collecting tweets with Python
Stop EC2 for specified time + start with Lambda (python)
Binarization with OpenCV / Python
Kernel Method with Python
Non-blocking with Python + uWSGI
python unittest assertXXX list
Posting tweets with python
How to run tests in bulk with Python unittest
Drive WebDriver with python
Use mecab with Python3
[Python] Redirect with CGIHTTPServer
Voice analysis with python
Think yaml with python
Operate Kinesis with Python
Getting Started with Python
Use DynamoDB with Python
Zundko getter with python
Handle Excel with python
Ohm's Law with Python
Primality test with python
Run Blender with python
Solve Sudoku with Python
Python starting with Windows 7