music21.test.testRunner

The testRunner module contains the all important “mainTest” function that runs tests in a given module. Except for the one instance of “defaultImports”, everything here can run on any system, not just music21.

Functions

music21.test.testRunner.addDocAttrTestsToSuite(suite, moduleVariableLists, outerFilename=None, globs=False, optionflags=12)

takes a suite, such as a doctest.DocTestSuite and the list of variables in a module and adds from those classes that have a _DOC_ATTR dictionary (which documents the properties in the class) any doctests to the suite.

>>> import doctest
>>> s1 = doctest.DocTestSuite(chord)
>>> s1TestsBefore = len(s1._tests)
>>> allLocals = [getattr(chord, x) for x in dir(chord)]
>>> test.testRunner.addDocAttrTestsToSuite(s1, allLocals)
>>> s1TestsAfter = len(s1._tests)
>>> s1TestsAfter - s1TestsBefore
1
>>> t = s1._tests[-1]
>>> t
isRest ()
music21.test.testRunner.fixTestsForPy2and3(doctestSuite)

Fix doctests so that they work in both python2 and python3, namely unicode/byte characters and added module names to exceptions.

>>> import doctest
>>> s1 = doctest.DocTestSuite(chord)
>>> test.testRunner.fixTestsForPy2and3(s1)
music21.test.testRunner.mainTest(*testClasses, **kwargs)

Takes as its arguments modules (or a string ‘noDocTest’ or ‘verbose’) and runs all of these modules through a unittest suite

Unless ‘noDocTest’ is passed as a module, a docTest is also performed on __main__, hence the name “mainTest”.

If ‘moduleRelative’ (a string) is passed as a module, then global variables are preserved.

Run example (put at end of your modules):

import unittest
class Test(unittest.TestCase):
    def testHello(self):
        hello = "Hello"
        self.assertEqual("Hello", hello)

import music21
if __name__ == '__main__':
    music21.mainTest(Test)

This module tries to fix up some differences between python2 and python3 so that the same doctests can work.

music21.test.testRunner.stripAddresses(textString, replacement='ADDRESS')

Function that changes all memory addresses (pointers) in the given textString with (replacement). This is useful for testing that a function gives an expected result even if the result contains references to memory locations. So for instance:

>>> test.testRunner.stripAddresses("{0.0} <music21.clef.TrebleClef object at 0x02A87AD0>")
'{0.0} <music21.clef.TrebleClef object at ADDRESS>'

while this is left alone:

>>> test.testRunner.stripAddresses("{0.0} <music21.humdrum.MiscTandem *>I humdrum control>")
'{0.0} <music21.humdrum.MiscTandem *>I humdrum control>'

For doctests, can strip to ‘...’ to make it work fine with doctest.ELLIPSIS

>>> test.testRunner.stripAddresses("{0.0} <music21.base.Music21Object object at 0x102a0ff10>", '0x...')
'{0.0} <music21.base.Music21Object object at 0x...>'
Return type:str