How to run tests in bulk with Python unittest

Introduction

I explained how to use unittest in Run unittest in Python (for beginners). While using it, I wanted to test multiple tests at once and organized them. In other words, how to use TestSuite. Official documentation and "[[Python] Two ways to implement a test suite with the unittest module](http://www.yoheim. net / blog.php? q = 20160903) ”was used as a reference.

Directory structure

.
├─sample_a.py
└─tests
  ├─__init__.py
  ├─test_samplea.py
  ├─test_sampleb.py
  ├─samplec.py
  └─sample_suite.py

Sample source

sample_a.py


class SampleA():
  def hello(self):
    print('Hello, I am sample of a!')

test_samplea.py


import unittest

from sample_a import SampleA


class TestSampleA(unittest.TestCase):
  def test_a1(self):
    print('Test sample A1')
    a = SampleA()
    a.hello()

  def test_a2(self):
    print('Test sample A2')

  def a3(self):
    print('Test sample A3')

test_sampleb.py


import unittest


class TestSampleB(unittest.TestCase):
  def test_b1(self):
    print('Test sample B1')

  def test_b2(self):
    print('Test sample B2')

  def b3(self):
    print('Test sample B3')

samplec.py


import unittest


class SampleC(unittest.TestCase):
  def test_c1(self):
    print('Test sample C1')

  def test_c2(self):
    print('Test sample C2')

  def c3(self):
    print('Test sample C3')

sample_suite.py


import unittest

from . import test_samplea
from . import test_sampleb
from . import samplec


def suite():
  suite = unittest.TestSuite()
  suite.addTest(test_samplea.TestSampleA('test_a1'))
  suite.addTest(test_samplea.TestSampleA('a3'))
  suite.addTest(unittest.makeSuite(test_sampleb.TestSampleB))
  suite.addTest(samplec.SampleC('test_c1'))
  return suite


if __name__ == '__main__':
  runner = unittest.TextTestRunner()
  test_suite = suite()
  runner.run(test_suite)

Run one test

python -m unittest tests.test_samplea The execution result is as follows.

Test sample A1
Hello, I am sample of a!
.Test sample A2
.
----------------------------------------------------------------------
Ran 2 tests in 0.003s

OK

From the output, you can see that the test methods test_a1 and test_a2 are being executed. The method of a3 has not been tested.

Run tests together

python -m unittest The execution result is as follows.

Test sample A1
Hello, I am sample of a!
.Test sample A2
.Test sample B1
.Test sample B2
.
----------------------------------------------------------------------
Ran 4 tests in 0.006s

OK

From the test results, you can see that the tests for test_samplea.py and test_sampleb.py are running. The test for samplec.py has not been run. A file that starts with test runs all the tests for the methods that start with test.

Run only your own tests

python -m tests.sample_suite

Test sample A1
Hello, I am sample of a!
.Test sample A3
.Test sample B1
.Test sample B2
.Test sample C1
.
----------------------------------------------------------------------
Ran 5 tests in 0.006s

OK

The test added by addTest is running. Test methods (a3) that do not start with test and test files (samplec.py) that do not start with test are also being executed. How to add a test is to add for each method suite.addTest(test_samplea.TestSampleA('test_a1')) Or Addition by class suite.addTest(unittest.makeSuite(test_sampleb.TestSampleB)) there is.

Summary

The test discovery method is very convenient because you can run the test without setting anything. However, when the number of tests increases or tests that take a long time to process are added, I want to exclude the tests, so I investigated how to execute only the tests that I decided. I couldn't make a trial and error and thought that it could be implemented with a little simpler implementation and executed with python -m unit test. It's not good because the interface to run is inconsistent. .. .. Please let me know if there is a simpler way to implement it.

Recommended Posts

How to run tests in bulk with Python unittest
How to run setUp only once in python unittest
How to work with BigQuery in Python
[REAPER] How to play with Reascript in Python
Convert PDFs to images in bulk with Python
How to use tkinter with python in pyenv
How to run Leap Motion in non-Apple Python
How to run Notepad ++ Python
How to develop in Python
How to convert / restore a string with [] in python
How to do hash calculation with salt in Python
Explain in detail how to make sounds with python
How to run python in virtual space (for MacOS)
[Python] How to do PCA in Python
Python: How to use async with
How to collect images in Python
How to get started with Python
How to kill processes in bulk
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
How to use FTP with Python
How to use PubChem in Python
How to calculate date with python
How to run TensorFlow 1.0 code in 2.0
How to handle Japanese in Python
How to test that Exception is raised in python unittest
[Road to intermediate Python] Install packages in bulk with pip
[TensorFlow 2 / Keras] How to run learning with CTC Loss in Keras
How to extract any appointment in Google Calendar with Python
How to do Bulk Update with PyMySQL and notes [Python]
How to run an app built with Python + py2app built with Anaconda
How to log in to AtCoder with Python and submit automatically
How to deal with python installation error in pyenv (BUILD FAILED)
[Introduction to Python] How to use class in Python?
Try logging in to qiita with Python
How to install OpenCV on Cloud9 and run it in Python
How to access environment variables in Python
How to dynamically define variables in Python
How to do R chartr () in Python
Unittest in python
[Itertools.permutations] How to put permutations in Python
How to create a heatmap with an arbitrary domain in Python
How to use python put in pyenv on macOS with PyCall
How to get a stacktrace in python
How to display multiplication table in python
How to extract polygon area in Python
How to do portmanteau test with python
How to check opencv version in python
How to display python Japanese with lolipop
[Python2.7] Summary of how to use unittest
How to switch python versions in cloud9
How to adjust image contrast in Python
How to use __slots__ in Python class
How to dynamically zero pad in Python
How to enter Japanese with Python curses
To work with timestamp stations in Python
How to use regular expressions in Python
[Python] How to deal with module errors
How to display Hello world in python
How to use is and == in Python