oryx.internal.test_util.TestCase

Stay organized with collections Save and categorize content based on your preferences.

Base class for test cases using the parameters decorator.

Child Classes

class failureException

Methods

addClassCleanup

Same as addCleanup, except the cleanup items are called even if setUpClass fails (unlike tearDownClass).

addCleanup

Add a function, with arguments, to be called when the test is completed. Functions added are called on a LIFO basis and are called after tearDown on test failure or success.

Cleanup items are called even if setUp fails (unlike tearDown).

addTypeEqualityFunc

Add a type specific assertEqual style function to compare a type.

This method is for use by TestCase subclasses that need to register their own type equality functions to provide nicer error messages.

Args
typeobj The data type to call this function on when both values are of the same type in assertEqual().
function The callable taking two arguments and an optional msg= argument that raises self.failureException with a useful error message when the two arguments are not equal.

assertAlmostEqual

Fail if the two objects are unequal as determined by their difference rounded to the given number of decimal places (default 7) and comparing to zero, or by comparing that the difference between the two objects is more than the given delta.

Note that decimal places (from zero) are usually not the same as significant digits (measured from the most significant digit).

If the two objects compare equal then they will automatically compare almost equal.

assertBetween

Asserts that value is between minv and maxv (inclusive).

assertCommandFails

Asserts a shell command fails and the error matches a regex in a list.

Args
command List or string representing the command to run.
regexes the list of regular expression strings.
env Dictionary of environment variable settings. If None, no environment variables will be set for the child process. This is to make tests more hermetic. NOTE: this behavior is different than the standard subprocess module.
close_fds Whether or not to close all open fd's in the child after forking.
msg Optional message to report on failure.

assertCommandSucceeds

Asserts that a shell command succeeds (i.e. exits with code 0).

Args
command List or string representing the command to run.
regexes List of regular expression byte strings that match success.
env Dictionary of environment variable settings. If None, no environment variables will be set for the child process. This is to make tests more hermetic. NOTE: this behavior is different than the standard subprocess module.
close_fds Whether or not to close all open fd's in the child after forking.
msg Optional message to report on failure.

assertContainsExactSubsequence

Asserts that "container" contains "subsequence" as an exact subsequence.

Asserts that "container" contains all the elements of "subsequence", in order, and without other elements interspersed. For example, [1, 2, 3] is an exact subsequence of [0, 0, 1, 2, 3, 0] but not of [0, 0, 1, 2, 0, 3, 0].

Args
container the list we're testing for subsequence inclusion.
subsequence the list we hope will be an exact subsequence of container.
msg Optional message to report on failure.

assertContainsInOrder

Asserts that the strings provided are found in the target in order.

This may be useful for checking HTML output.

Args
strings A list of strings, such as [ 'fox', 'dog' ]
target A target string in which to look for the strings, such as 'The quick brown fox jumped over the lazy dog'.
msg Optional message to report on failure.

assertContainsSubsequence

Asserts that "container" contains "subsequence" as a subsequence.

Asserts that "container" contains all the elements of "subsequence", in order, but possibly with other elements interspersed. For example, [1, 2, 3] is a subsequence of [0, 0, 1, 2, 0, 3, 0] but not of [0, 0, 1, 3, 0, 2, 0].

Args
container the list we're testing for subsequence inclusion.
subsequence the list we hope will be a subsequence of container.
msg Optional message to report on failure.

assertContainsSubset

Checks whether actual iterable is a superset of expected iterable.

assertCountEqual

Asserts that two iterables have the same elements, the same number of times, without regard to order.

self.assertEqual(Counter(list(first)),
Counter(list(second)))


Example:

- [0, 1, 1] and [1, 0, 1] compare equal.
- [0, 0, 1] and [0, 1] compare unequal.


assertDictContainsSubset

Checks whether dictionary is a superset of subset.

assertDictEqual

Raises AssertionError if a and b are not equal dictionaries.

Args
a A dict, the expected value.
b A dict, the actual value.
msg An optional str, the associated message.

Raises
AssertionError if the dictionaries are not equal.

assertEmpty

Asserts that an object has zero length.

Args
container Anything that implements the collections.abc.Sized interface.
msg Optional message to report on failure.

assertEndsWith

Asserts that actual.endswith(expected_end) is True.

Args
actual str
expected_end str
msg Optional message to report on failure.

assertEqual

Fail if the two objects are unequal as determined by the '==' operator.

assertFalse

Check that the expression is false.

assertGreater

Just like self.assertTrue(a > b), but with a nicer default message.

assertGreaterEqual

Just like self.assertTrue(a >= b), but with a nicer default message.

assertIn

Just like self.assertTrue(a in b), but with a nicer default message.

assertIs

Just like self.assertTrue(a is b), but with a nicer default message.

assertIsInstance

Same as self.assertTrue(isinstance(obj, cls)), with a nicer default message.

assertIsNone

Same as self.assertTrue(obj is None), with a nicer default message.

assertIsNot

Just like self.assertTrue(a is not b), but with a nicer default message.

assertIsNotNone

Included for symmetry with assertIsNone.

assertItemsEqual

This is equivalent to assertCountEqual.

Args
expected_seq A sequence containing elements we are expecting.
actual_seq The sequence that we are testing.
msg The message to be printed if the test fails.

assertJsonEqual

Asserts that the JSON objects defined in two strings are equal.

A summary of the differences will be included in the failure message using assertSameStructure.

Args
first A string containing JSON to decode and compare to second.
second A string containing JSON to decode and compare to first.
msg Additional text to include in the failure message.

assertLen

Asserts that an object has the expected length.

Args
container Anything that implements the collections.abc.Sized interface.
expected_len The expected length of the container.
msg Optional message to report on failure.

assertLess

Just like self.assertTrue(a < b), but with a nicer default message.

assertLessEqual

Just like self.assertTrue(a <= b), but with a nicer default message.

assertListEqual

A list-specific equality assertion.

Args
list1 The first list to compare.
list2 The second list to compare.
msg Optional message to use on failure instead of a list of differences.

assertLogs

Fail unless a log message of level level or higher is emitted on logger_name or its children. If omitted, level defaults to INFO and logger defaults to the root logger.

This method must be used as a context manager, and will yield a recording object with two attributes: output and records. At the end of the context manager, the output attribute will be a list of the matching formatted log messages and the records attribute will be a list of the corresponding LogRecord objects.

Example::

with self.assertLogs('foo', level='INFO') as cm:
logging.getLogger('foo').info('first message')
logging.getLogger('foo.bar').error('second message')
self.assertEqual(cm.output, ['INFO:foo:first message',
'ERROR:foo.bar:second message'])


assertMultiLineEqual

Asserts that two multi-line strings are equal.

assertNoCommonElements

Checks whether actual iterable and expected iterable are disjoint.

assertNotAlmostEqual

Fail if the two objects are equal as determined by their difference rounded to the given number of decimal places (default 7) and comparing to zero, or by comparing that the difference between the two objects is less than the given delta.

Note that decimal places (from zero) are usually not the same as significant digits (measured from the most significant digit).

Objects that are equal automatically fail.

assertNotEmpty

Asserts that an object has non-zero length.

Args
container Anything that implements the collections.abc.Sized interface.
msg Optional message to report on failure.

assertNotEndsWith

Asserts that actual.endswith(unexpected_end) is False.

Args
actual str
unexpected_end str
msg Optional message to report on failure.

assertNotEqual

Fail if the two objects are equal as determined by the '!=' operator.

assertNotIn

Just like self.assertTrue(a not in b), but with a nicer default message.

assertNotIsInstance

Included for symmetry with assertIsInstance.

assertNotRegex

Fail the test if the text matches the regular expression.

assertNotStartsWith

Asserts that actual.startswith(unexpected_start) is False.

Args
actual str
unexpected_start str
msg Optional message to report on failure.

assertRaises

Fail unless an exception of class expected_exception is raised by the callable when invoked with specified positional and keyword arguments. If a different type of exception is raised, it will not be caught, and the test case will be deemed to have suffered an error, exactly as for an unexpected exception.

If called with the callable and arguments omitted, will return a context object used like this::

 with self.assertRaises(SomeException):
do_something()


An optional keyword argument 'msg' can be provided when assertRaises is used as a context object.

The context manager keeps a reference to the exception as the 'exception' attribute. This allows you to inspect the exception after the assertion::

with self.assertRaises(SomeException) as cm:
do_something()
the_exception = cm.exception
self.assertEqual(the_exception.error_code, 3)


assertRaisesRegex

Asserts that the message in a raised exception matches a regex.

Args
expected_exception Exception class expected to be raised.
expected_regex Regex (re.Pattern object or string) expected to be found in error message.
args Function to be called and extra positional args.
kwargs Extra kwargs.
msg Optional message used in case of failure. Can only be used when assertRaisesRegex is used as a context manager.

assertRaisesWithLiteralMatch

Asserts that the message in a raised exception equals the given string.

Unlike assertRaisesRegex, this method takes a literal string, not a regular expression.

with self.assertRaisesWithLiteralMatch(ExType, 'message'): DoSomething()

Args
expected_exception Exception class expected to be raised.
expected_exception_message String message expected in the raised exception. For a raise exception e, expected_exception_message must equal str(e).
callable_obj Function to be called, or None to return a context.
*args Extra args.
**kwargs Extra kwargs.

Returns
A context manager if callable_obj is None. Otherwise, None.

Raises
self.failureException if callable_obj does not raise a matching exception.

assertRaisesWithPredicateMatch

Asserts that exception is thrown and predicate(exception) is true.

Args
expected_exception Exception class expected to be raised.
predicate Function of one argument that inspects the passed-in exception and returns True (success) or False (please fail the test).
callable_obj Function to be called.
*args Extra args.
**kwargs Extra keyword args.

Returns
A context manager if callable_obj is None. Otherwise, None.

Raises
self.failureException if callable_obj does not raise a matching exception.

assertRegex

Fail the test unless the text matches the regular expression.

assertRegexMatch

Asserts that at least one regex in regexes matches str.

If possible you should use assertRegex, which is a simpler version of this method. assertRegex takes a single regular expression (a string or re compiled object) instead of a list.

Notes:

1. This function uses substring matching, i.e. the matching succeeds if any substring of the error message matches any regex in the list. This is more convenient for the user than full-string matching.

2. If regexes is the empty list, the matching will always fail.

3. Use regexes=[''] for a regex that will always pass.

4. '.' matches any single character except the newline. To match any character, use '(.|\n)'.

5. '^' matches the beginning of each line, not just the beginning of the string. Similarly, '\$' matches the end of each line.

6. An exception will be thrown if regexes contains an invalid regex.

Args
actual_str The string we try to match with the items in regexes.
regexes The regular expressions we want to match against str. See "Notes" above for detailed notes on how this is interpreted.
message The message to be printed if the test fails.

assertSameElements

Asserts that two sequences have the same elements (in any order).

This method, unlike assertCountEqual, doesn't care about any duplicates in the expected and actual sequences::

# Doesn't raise an AssertionError
assertSameElements([1, 1, 1, 0, 0, 0], [0, 1])


If possible, you should use assertCountEqual instead of assertSameElements.

Args
expected_seq A sequence containing elements we are expecting.
actual_seq The sequence that we are testing.
msg The message to be printed if the test fails.

assertSameStructure

Asserts that two values contain the same structural content.

The two arguments should be data trees consisting of trees of dicts and lists. They will be deeply compared by walking into the contents of dicts and lists; other items will be compared using the == operator. If the two structures differ in content, the failure message will indicate the location within the structures where the first difference is found. This may be helpful when comparing large structures.

Mixed Sequence and Set types are supported. Mixed Mapping types are supported, but the order of the keys will not be considered in the comparison.

Args
a The first structure to compare.
b The second structure to compare.
aname Variable name to use for the first structure in assertion messages.
bname Variable name to use for the second structure.
msg Additional text to include in the failure message.

assertSequenceAlmostEqual

An approximate equality assertion for ordered sequences.

Fail if the two sequences are unequal as determined by their value differences rounded to the given number of decimal places (default 7) and comparing to zero, or by comparing that the difference between each value in the two sequences is more than the given delta.

Note that decimal places (from zero) are usually not the same as significant digits (measured from the most significant digit).

If the two sequences compare equal then they will automatically compare almost equal.

Args
expected_seq A sequence containing elements we are expecting.
actual_seq The sequence that we are testing.
places The number of decimal places to compare.
msg The message to be printed if the test fails.
delta The OK difference between compared values.

assertSequenceEqual

An equality assertion for ordered sequences (like lists and tuples).

For the purposes of this function, a valid ordered sequence type is one which can be indexed, has a length, and has an equality operator.

Args
seq1 The first sequence to compare.
seq2 The second sequence to compare.
seq_type The expected datatype of the sequences, or None if no datatype should be enforced.
msg Optional message to use on failure instead of a list of differences.

assertSequenceStartsWith

An equality assertion for the beginning of ordered sequences.

If prefix is an empty sequence, it will raise an error unless whole is also an empty sequence.

If prefix is not a sequence, it will raise an error if the first element of whole does not match.

Args
prefix A sequence expected at the beginning of the whole parameter.
whole The sequence in which to look for prefix.
msg Optional message to report on failure.

assertSetEqual

A set-specific equality assertion.

Args
set1 The first set to compare.
set2 The second set to compare.
msg Optional message to use on failure instead of a list of differences.

assertSetEqual uses ducktyping to support different types of sets, and is optimized for sets specifically (parameters must support a difference method).

assertStartsWith

Asserts that actual.startswith(expected_start) is True.

Args
actual str
expected_start str
msg Optional message to report on failure.

assertTotallyOrdered

Asserts that total ordering has been implemented correctly.

For example, say you have a class A that compares only on its attribute x. Comparators other than __lt__ are omitted for brevity::

class A(object):
def __init__(self, x, y):
self.x = x
self.y = y

def __hash__(self):
return hash(self.x)

def __lt__(self, other):
try:
return self.x < other.x
except AttributeError:
return NotImplemented


assertTotallyOrdered will check that instances can be ordered correctly. For example::

self.assertTotallyOrdered(
[None],  # None should come before everything else.
[1],  # Integers sort earlier.
[A(1, 'a')],
[A(2, 'b')],  # 2 is after 1.
[A(3, 'c'), A(3, 'd')],  # The second argument is irrelevant.
[A(4, 'z')],
['foo'])  # Strings sort last.


Args
*groups A list of groups of elements. Each group of elements is a list of objects that are equal. The elements in each group must be less than the elements in the group after it. For example, these groups are totally ordered: [None], [1], [2, 2], [3].
**kwargs optional msg keyword argument can be passed.

assertTrue

Check that the expression is true.

assertTupleEqual

A tuple-specific equality assertion.

Args
tuple1 The first tuple to compare.
tuple2 The second tuple to compare.
msg Optional message to use on failure instead of a list of differences.

assertUrlEqual

Asserts that urls are equal, ignoring ordering of query params.

assertWarns

Fail unless a warning of class warnClass is triggered by the callable when invoked with specified positional and keyword arguments. If a different type of warning is triggered, it will not be handled: depending on the other warning filtering rules in effect, it might be silenced, printed out, or raised as an exception.

If called with the callable and arguments omitted, will return a context object used like this::

 with self.assertWarns(SomeWarning):
do_something()


An optional keyword argument 'msg' can be provided when assertWarns is used as a context object.

The context manager keeps a reference to the first matching warning as the 'warning' attribute; similarly, the 'filename' and 'lineno' attributes give you information about the line of Python code from which the warning was triggered. This allows you to inspect the warning after the assertion::

with self.assertWarns(SomeWarning) as cm:
do_something()
the_warning = cm.warning
self.assertEqual(the_warning.some_attribute, 147)


assertWarnsRegex

Asserts that the message in a triggered warning matches a regexp. Basic functioning is similar to assertWarns() with the addition that only warnings whose messages also match the regular expression are considered successful matches.

Args
expected_warning Warning class expected to be triggered.
expected_regex Regex (re.Pattern object or string) expected to be found in error message.
args Function to be called and extra positional args.
kwargs Extra kwargs.
msg Optional message used in case of failure. Can only be used when assertWarnsRegex is used as a context manager.

create_tempdir

Create a temporary directory specific to the test.

This creates a named directory on disk that is isolated to this test, and will be properly cleaned up by the test. This avoids several pitfalls of creating temporary directories for test purposes, as well as makes it easier to setup directories and verify their contents. For example::

def test_foo(self):
out_dir = self.create_tempdir()
out_log = out_dir.create_file('output.log')
expected_outputs = [
os.path.join(out_dir, 'data-0.txt'),
os.path.join(out_dir, 'data-1.txt'),
]
code_under_test(out_dir)
self.assertTrue(os.path.exists(expected_paths[0]))
self.assertTrue(os.path.exists(expected_paths[1]))


See also: :meth:create_tempfile for creating temporary files.

Args
name Optional name of the directory. If not given, a unique name will be generated and used.
cleanup Optional cleanup policy on when/if to remove the directory (and all its contents) at the end of the test. If None, then uses :attr:tempfile_cleanup.

Returns
A _TempDir representing the created directory; see _TempDir class docs for usage.

create_tempfile

Create a temporary file specific to the test.

This creates a named file on disk that is isolated to this test, and will be properly cleaned up by the test. This avoids several pitfalls of creating temporary files for test purposes, as well as makes it easier to setup files, their data, read them back, and inspect them when a test fails. For example::

def test_foo(self):
output = self.create_tempfile()
code_under_test(output)
self.assertGreater(os.path.getsize(output), 0)


See also: :meth:create_tempdir for creating temporary directories, and _TempDir.create_file for creating files within a temporary directory.

Args
file_path Optional file path for the temp file. If not given, a unique file name will be generated and used. Slashes are allowed in the name; any missing intermediate directories will be created. NOTE: This path is the path that will be cleaned up, including any directories in the path, e.g., 'foo/bar/baz.txt' will rm -r foo.
content Optional string or bytes to initially write to the file. If not specified, then an empty file is created.
mode Mode string to use when writing content. Only used if content is non-empty.
encoding Encoding to use when writing string content. Only used if content is text.
errors How to handle text to bytes encoding errors. Only used if content is text.
cleanup Optional cleanup policy on when/if to remove the directory (and all its contents) at the end of the test. If None, then uses :attr:tempfile_cleanup.

Returns
A _TempFile representing the created file; see _TempFile class docs for usage.

debug

Run the test without collecting errors in a TestResult

doClassCleanups

Execute all class cleanup functions. Normally called for you after tearDownClass.

doCleanups

Execute all cleanup functions. Normally called for you after tearDown.

fail

Fail immediately with the given message, optionally prefixed.

id

Returns the descriptive ID of the test.

This is used internally by the unittesting framework to get a name for the test to be used in reports.

Returns
The test id.

setUp

Hook method for setting up the test fixture before exercising it.

setUpClass

Hook method for setting up class fixture before running tests in the class.

shortDescription

Formats both the test method name and the first line of its docstring.

If no docstring is given, only returns the method name.

This method overrides unittest.TestCase.shortDescription(), which only returns the first line of the docstring, obscuring the name of the test upon failure.

Returns
desc A short description of a test method.

Skip this test.

subTest

Return a context manager that will return the enclosed block of code in a subtest identified by the optional message and keyword parameters. A failure in the subtest marks the test case as failed but resumes execution at the end of the enclosed block, allowing further test code to be executed.

tearDown

Hook method for deconstructing the test fixture after testing it.

tearDownClass

Hook method for deconstructing the class fixture after running all tests in the class.

__call__

Call self as a function.

__eq__

Return self==value.

longMessage True
maxDiff 1600
tempfile_cleanup <TempFileCleanup.ALWAYS: 'always'>

[{ "type": "thumb-down", "id": "missingTheInformationINeed", "label":"Missing the information I need" },{ "type": "thumb-down", "id": "tooComplicatedTooManySteps", "label":"Too complicated / too many steps" },{ "type": "thumb-down", "id": "outOfDate", "label":"Out of date" },{ "type": "thumb-down", "id": "samplesCodeIssue", "label":"Samples / code issue" },{ "type": "thumb-down", "id": "otherDown", "label":"Other" }]
[{ "type": "thumb-up", "id": "easyToUnderstand", "label":"Easy to understand" },{ "type": "thumb-up", "id": "solvedMyProblem", "label":"Solved my problem" },{ "type": "thumb-up", "id": "otherUp", "label":"Other" }]