brennivin.testhelpers module

Auxilliary classes to facilitate testing.

Members

unittest: Points to the unittest module in Python >= 2.7, and unittest2 in Python <= 2.6. This aids in creating version-agnostic test cases.

class brennivin.testhelpers.CallCounter(call)

Counts the number of times the instance is called. Available via the count attribute.

Generally you should not create this class directly, and use the no_params and all_params class methods. Use the former when calling this should take no arguments, and the latter when it should take any arguments.

class brennivin.testhelpers.FakeTestCase

Sometimes, you want to use one of the assertion methods in this module, but don’t have a testcase. You can just use an instance of this.

class brennivin.testhelpers.Patcher(obj, attrname, newvalue=DEFAULT)

Context manager that stores getattr(obj, attrname), sets it to newvalue on enter, and restores it on exit.

Parameters:newvalue – If _undefined, use a new Mock.
brennivin.testhelpers.assertBetween(tc, a, b, c, eq=False)

Asserts that:

if eq:
    a <= b <= c
else:
    a < b < c
brennivin.testhelpers.assertCrcEqual(testcase, calcpath, idealpath, asLib=False)

Asserts if crcs of paths are not equal. If DIFF_FILES_ON_CRC_FAIL is True, launch P4MERGE to diff the files.

Parameters:asLib – If True, do not print or show diff (function is being used as part of another assert function and not as a standalone).
brennivin.testhelpers.assertEqualPretty(testcase, calculated, ideal, msg=None)

Prints ideal and calculated on two lines, for easier analysis of what’s different. Useful for sequences.

Parameters:
  • testcase – An instance of unittest.TestCase
  • ideal – The value that should have been calculated.
  • calculated – the value that was calculated.
brennivin.testhelpers.assertFoldersEqual(testcase, calcfolder, idealfolder, compare=assertCrcEqual)

Asserts if any differences are found between two folders.

Parameters:compare – Assertion method to use. Pass in a custom function that switches off of extensions if you want.
brennivin.testhelpers.assertJsonEqual(calc, ideal, out=<open file '<stderr>', mode 'w' at 0x7f38bfb101e0>)

Asserts if calc != ideal. Will print the diff between the json dump of calc and ideal to out before asserting, as a debugging aid.

brennivin.testhelpers.assertNumberSequencesEqual(testcase, a, b, tolerance=0)

Assert that for an element in sequence a the corresponding element in b is equal within tolerance.

Also assert if the two sequences are not the same length.

brennivin.testhelpers.assertNumbersEqual(testcase, a, b, tolerance=0, msg=None)

Asserts if the sizes are not within tolerance.

brennivin.testhelpers.assertPermissionbitsEqual(testcase, calcpath, idealpath, bitgetter=os.stat(<path>)[0])

Asserts if permission bits are not equal.

brennivin.testhelpers.assertTextFilesEqual(testcase, calcpath, idealpath, compareLines=None)

Asserts if the files are not equal. It first compares crcs, and if that fails, it compares the file contents as text (ie, mode ‘r’ and not ‘rb’). The reason for this is that there can be discrepancies between newlines.

Parameters:compareLines – Callable that takes (calculated line, ideal line) and should assert if they are not equal. Defaults to testcase.assertEqual(calc line, ideal line).
brennivin.testhelpers.assertXmlEqual(a, b)

Asserts two xml documents are equal.

brennivin.testhelpers.compareXml(x1, x2, reporter=lambda x: x)

Compares two xml elements. If they are equal, return True. If not, return False. Differences are reported by calling the reporter parameter, such as

reporter('Tags do not match: Foo and Bar')
brennivin.testhelpers.patch(testcase, *args)

Create and enter Patcher that will be exited on testcase teardown.

Parameters:args – Passed to the Patcher.
Returns:The monkey patcher’s newvalue.