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 ()
>>> 'hi' 'hi'
-
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 >>> suite1 = doctest.DocTestSuite(chord) >>> doctestCase = list(iter(suite1))[0] >>> dt = doctestCase._dt_test >>> testWithTraceback = None >>> for testExample in dt.examples: ... if testExample.exc_msg is not None: ... testWithTraceback = testExample ... break
Py3 example:
<<< testWithTraceback.exc_msg “ChordException: Could not process input argumentn” <<< test.testRunner.fixTestsForPy2and3(suite1) <<< testWithTraceback.exc_msg ”...ChordException: Could not process input argumentn”
Py2 example:
<<< testWithTraceback.exc_msg “music21.chord.ChordException: Could not process input argumentn” <<< test.testRunner.fixTestsForPy2and3(suite1) <<< testWithTraceback.exc_msg “ChordException: Could not process input argumentn”
-
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
Py3In2OutputChecker¶
-
class
music21.test.testRunner.
Py3In2OutputChecker
¶ In music21, we write all doctests for passing under Python 3. The differences between it and Py2 mean that we need to find certain differences and remove them.
First version: removes bytes from the expected output (want) and unicode from received (got)
Py3In2OutputChecker
methods
-
Py3In2OutputChecker.
check_output
(want, got, optionflags)¶ cannot use super with Py2 since we have old-style classes going on.