Change language

Python | Registering test results to a file

|

A very common technique for running unit tests — include a small piece of code (as shown in the code below) at the bottom of the test file.

Code # 1:

import unittest

class MyTest ( unittest.TestCase):

...

if __ name__ = = ’__main__’ :

  unittest.main ()

This makes the test executable and prints the test execution results to standard output. To redirect this output, unwind the main () call a bit and write your own main () function as shown in the code below:

Code # 2:

import sys

 

def main (out = sys.stderr, verbosity = 2 ) :

loader = unittest.TestLoader ()

 

  suite = loader.loadTestsFrom Module (sys.modules [__ name__])

unittest.TextTestRunner (out, verbosity = verbosity) .run (suite)

 

if __ name__ = = ’__main__’ :

with open ( ’testing.out’ , ’ w’ ) as f:

  main (f)

How it works:

  • The interesting thing in the code is not so much the problem of redirecting the results The fact that this exposes some of the notable inner workings of the unittest module.
  • At a basic level, the unittest module works by building a suite of tests first.
  • This test suite consists of various test methods that you have defined. Once a package is built, its tests are run.
  • These two parts of unit testing are separate from each other. The unittest.TestLoader instance created in the solution is used to build the test suite.
  • loadTestsFromModule () is one of several methods it defines for collecting tests ... In this case, it scans the module for TestCase classes and extracts test methods from them.
  • The loadTestsFromTestCase () method (not shown) can be used to extract test methods from a separate class, which inherits from TestCase.
  • The TextTestRunner class is an example of a test runner class. The main purpose of this class is — run the tests contained in the test suite. This class — the same test runner that sits behind the unittest.main () function.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically