Skip to content

Instantly share code, notes, and snippets.

@alaudet
Created June 8, 2019 12:40
Show Gist options
  • Save alaudet/080c74134c80f166f7082a7f7ad0c164 to your computer and use it in GitHub Desktop.
Save alaudet/080c74134c80f166f7082a7f7ad0c164 to your computer and use it in GitHub Desktop.
Nose Tests Pydocs
NAME
nose.tools
DESCRIPTION
Tools for testing
-----------------
nose.tools provides a few convenience functions to make writing tests
easier. You don't have to use them; nothing in the rest of nose depends
on any of these methods.
PACKAGE CONTENTS
nontrivial
trivial
CLASSES
builtins.AssertionError(builtins.Exception)
nose.tools.nontrivial.TimeExpired
class TimeExpired(builtins.AssertionError)
| Assertion failed.
|
| Method resolution order:
| TimeExpired
| builtins.AssertionError
| builtins.Exception
| builtins.BaseException
| builtins.object
|
| Data descriptors defined here:
|
| __weakref__
| list of weak references to the object (if defined)
|
| ----------------------------------------------------------------------
| Methods inherited from builtins.AssertionError:
|
| __init__(self, /, *args, **kwargs)
| Initialize self. See help(type(self)) for accurate signature.
|
| __new__(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.
|
| ----------------------------------------------------------------------
| Methods inherited from builtins.BaseException:
|
| __delattr__(self, name, /)
| Implement delattr(self, name).
|
| __getattribute__(self, name, /)
| Return getattr(self, name).
|
| __reduce__(...)
| helper for pickle
|
| __repr__(self, /)
| Return repr(self).
|
| __setattr__(self, name, value, /)
| Implement setattr(self, name, value).
|
| __setstate__(...)
|
| __str__(self, /)
| Return str(self).
|
| with_traceback(...)
| Exception.with_traceback(tb) --
| set self.__traceback__ to tb and return self.
|
| ----------------------------------------------------------------------
| Data descriptors inherited from builtins.BaseException:
|
| __cause__
| exception cause
|
| __context__
| exception context
|
| __dict__
|
| __suppress_context__
|
| __traceback__
|
| args
FUNCTIONS
assert_almost_equal = assertAlmostEqual(first, second, places=None, msg=None, delta=None) method of nose.tools.trivial.Dummy instance
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.
assert_almost_equals = deprecated_func(*args, **kwargs) method of nose.tools.trivial.Dummy instance
assert_count_equal = assertCountEqual(first, second, msg=None) method of nose.tools.trivial.Dummy instance
An unordered sequence comparison asserting that the same elements,
regardless of order. If the same element occurs more than once,
it verifies that the elements occur the same number of times.
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.
assert_dict_contains_subset = assertDictContainsSubset(subset, dictionary, msg=None) method of nose.tools.trivial.Dummy instance
Checks whether dictionary is a superset of subset.
assert_dict_equal = assertDictEqual(d1, d2, msg=None) method of nose.tools.trivial.Dummy instance
assert_equal = assertEqual(first, second, msg=None) method of nose.tools.trivial.Dummy instance
Fail if the two objects are unequal as determined by the '=='
operator.
assert_equals = deprecated_func(*args, **kwargs) method of nose.tools.trivial.Dummy instance
assert_false = assertFalse(expr, msg=None) method of nose.tools.trivial.Dummy instance
Check that the expression is false.
assert_greater = assertGreater(a, b, msg=None) method of nose.tools.trivial.Dummy instance
Just like self.assertTrue(a > b), but with a nicer default message.
assert_greater_equal = assertGreaterEqual(a, b, msg=None) method of nose.tools.trivial.Dummy instance
Just like self.assertTrue(a >= b), but with a nicer default message.
assert_in = assertIn(member, container, msg=None) method of nose.tools.trivial.Dummy instance
Just like self.assertTrue(a in b), but with a nicer default message.
assert_is = assertIs(expr1, expr2, msg=None) method of nose.tools.trivial.Dummy instance
Just like self.assertTrue(a is b), but with a nicer default message.
assert_is_instance = assertIsInstance(obj, cls, msg=None) method of nose.tools.trivial.Dummy instance
Same as self.assertTrue(isinstance(obj, cls)), with a nicer
default message.
assert_is_none = assertIsNone(obj, msg=None) method of nose.tools.trivial.Dummy instance
Same as self.assertTrue(obj is None), with a nicer default message.
assert_is_not = assertIsNot(expr1, expr2, msg=None) method of nose.tools.trivial.Dummy instance
Just like self.assertTrue(a is not b), but with a nicer default message.
assert_is_not_none = assertIsNotNone(obj, msg=None) method of nose.tools.trivial.Dummy instance
Included for symmetry with assertIsNone.
assert_less = assertLess(a, b, msg=None) method of nose.tools.trivial.Dummy instance
Just like self.assertTrue(a < b), but with a nicer default message.
assert_less_equal = assertLessEqual(a, b, msg=None) method of nose.tools.trivial.Dummy instance
Just like self.assertTrue(a <= b), but with a nicer default message.
assert_list_equal = assertListEqual(list1, list2, msg=None) method of nose.tools.trivial.Dummy instance
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.
assert_logs = assertLogs(logger=None, level=None) method of nose.tools.trivial.Dummy instance
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'])
assert_multi_line_equal = assertMultiLineEqual(first, second, msg=None) method of nose.tools.trivial.Dummy instance
Assert that two multi-line strings are equal.
assert_not_almost_equal = assertNotAlmostEqual(first, second, places=None, msg=None, delta=None) method of nose.tools.trivial.Dummy instance
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.
assert_not_almost_equals = deprecated_func(*args, **kwargs) method of nose.tools.trivial.Dummy instance
assert_not_equal = assertNotEqual(first, second, msg=None) method of nose.tools.trivial.Dummy instance
Fail if the two objects are equal as determined by the '!='
operator.
assert_not_equals = deprecated_func(*args, **kwargs) method of nose.tools.trivial.Dummy instance
assert_not_in = assertNotIn(member, container, msg=None) method of nose.tools.trivial.Dummy instance
Just like self.assertTrue(a not in b), but with a nicer default message.
assert_not_is_instance = assertNotIsInstance(obj, cls, msg=None) method of nose.tools.trivial.Dummy instance
Included for symmetry with assertIsInstance.
assert_not_regex = assertNotRegex(text, unexpected_regex, msg=None) method of nose.tools.trivial.Dummy instance
Fail the test if the text matches the regular expression.
assert_not_regexp_matches = deprecated_func(*args, **kwargs) method of nose.tools.trivial.Dummy instance
assert_raises = assertRaises(expected_exception, *args, **kwargs) method of nose.tools.trivial.Dummy instance
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)
assert_raises_regex = assertRaisesRegex(expected_exception, expected_regex, *args, **kwargs) method of nose.tools.trivial.Dummy instance
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.
assert_raises_regexp = deprecated_func(*args, **kwargs) method of nose.tools.trivial.Dummy instance
assert_regex = assertRegex(text, expected_regex, msg=None) method of nose.tools.trivial.Dummy instance
Fail the test unless the text matches the regular expression.
assert_regexp_matches = deprecated_func(*args, **kwargs) method of nose.tools.trivial.Dummy instance
assert_sequence_equal = assertSequenceEqual(seq1, seq2, msg=None, seq_type=None) method of nose.tools.trivial.Dummy instance
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.
assert_set_equal = assertSetEqual(set1, set2, msg=None) method of nose.tools.trivial.Dummy instance
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).
assert_true = assertTrue(expr, msg=None) method of nose.tools.trivial.Dummy instance
Check that the expression is true.
assert_tuple_equal = assertTupleEqual(tuple1, tuple2, msg=None) method of nose.tools.trivial.Dummy instance
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.
assert_warns = assertWarns(expected_warning, *args, **kwargs) method of nose.tools.trivial.Dummy instance
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)
assert_warns_regex = assertWarnsRegex(expected_warning, expected_regex, *args, **kwargs) method of nose.tools.trivial.Dummy instance
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.
eq_(a, b, msg=None)
Shorthand for 'assert a == b, "%r != %r" % (a, b)
istest(func)
Decorator to mark a function or method as a test
make_decorator(func)
Wraps a test decorator so as to properly replicate metadata
of the decorated function, including nose's additional stuff
(namely, setup and teardown).
nottest(func)
Decorator to mark a function or method as *not* a test
ok_(expr, msg=None)
Shorthand for assert. Saves 3 whole characters!
raises(*exceptions)
Test must raise one of expected exceptions to pass.
Example use::
@raises(TypeError, ValueError)
def test_raises_type_error():
raise TypeError("This test passes")
@raises(Exception)
def test_that_fails_by_passing():
pass
If you want to test many assertions about exceptions in a single test,
you may want to use `assert_raises` instead.
set_trace()
Call pdb.set_trace in the calling frame, first restoring
sys.stdout to the real output stream. Note that sys.stdout is NOT
reset to whatever it was before the call once pdb is done!
timed(limit)
Test must finish within specified time limit to pass.
Example use::
@timed(.1)
def test_that_fails():
time.sleep(.2)
with_setup(setup=None, teardown=None)
Decorator to add setup and/or teardown methods to a test function::
@with_setup(setup, teardown)
def test_something():
" ... "
Note that `with_setup` is useful *only* for test functions, not for test
methods or inside of TestCase subclasses.
DATA
__all__ = ['ok_', 'eq_', 'assert_almost_equal', 'assert_almost_equals'...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment