Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get nose to discover dynamically-generated testcases?

This is a follow-up to a previous question of mine.

In the previous question, methods were explored to implement what was essentially the same test over an entire family of functions, ensuring testing did not stop at the first function that failed.

My preferred solution used a metaclass to dynamically insert the tests into a unittest.TestCase. Unfortunately, nose does not pick this up because nose statically scans for test cases.

How do I get nose to discover and run such a TestCase? Please refer here for an example of the TestCase in question.

like image 222
saffsd Avatar asked Dec 07 '08 13:12

saffsd


1 Answers

Nose has a "test generator" feature for stuff like this. You write a generator function that yields each "test case" function you want it to run, along with its args. Following your previous example, this could check each of the functions in a separate test:

import unittest
import numpy

from somewhere import the_functions

def test_matrix_functions():
    for function in the_functions:
        yield check_matrix_function, function

def check_matrix_function(function)
    matrix1 = numpy.ones((5,10))
    matrix2 = numpy.identity(5)
    output = function(matrix1, matrix2)
    assert matrix1.shape == output.shape, \
           "%s produces output of the wrong shape" % str(function)
like image 93
Matt Good Avatar answered Oct 21 '22 22:10

Matt Good