Skip to content

Latest commit

 

History

History
63 lines (48 loc) · 7.6 KB

python-unit-test-with-nose.md

File metadata and controls

63 lines (48 loc) · 7.6 KB

Unit Test

單元測試採用 nose 套件來管理、執行。

API of unitest and nose

所有 nose 可用的 assert 名稱都跟 unitest.TestCase 底下的的方法名稱相同,但所有方法名稱都依照  PEP 8#function-names 重新命名。以下是可是用的方法列表(正確方法名稱請參考 unitest):

The following table lists the most commonly used methods:

Method In unitest Checks that Method In nose
assertEqual(a, b) a == b assert_equal(a, b)
assertNotEqual(a, b) a != b assert_not_equal(a, b)
assertTrue(x) bool(x) is True assert_true(x)
assertFalse(x) bool(x) is False assert_false(x)
assertIs(a, b) a is b assert_is(a, b)
assertIsNot(a, b) a is not b assert_is_not(a, b)
assertIsNone(x) x is None assert_is_none(x)
assertIsNotNone(x) x is not None assert_is_not_none(x)
assertIn(a, b) a in b assert_in(a, b)
assertNotIn(a, b) a not in b assert_not_in(a, b)
assertIsInstance(a, b) isinstance(a, b) assert_is_instance(a, b)
assertNotIsInstance(a, b) not isinstance(a, b) assert_is_not_instance(a, b)

It is also possible to check the production of exceptions, warnings, and log messages using the following methods:

Method In unitest Checks that Method In nose
assertRaises(exc, fun, *args, **kwds) fun(*args, **kwds) raises exc assert_raises(exc, fun, *args, **kwds)
assertRaisesRegex(exc, r, fun, *args, **kwds) fun(*args, **kwds) raises exc and the message matches regex r assert_raises_regex(exc, r, fun, *args, **kwds)
assertWarns(warn, fun, *args, **kwds) fun(*args, **kwds) raises warn assert_warns(warn, fun, *args, **kwds)
assertWarnsRegex(warn, r, fun, *args, **kwds) fun(*args, **kwds) raises warn and the message matches regex r assert_warns_regex(warn, r, fun, *args, **kwds)
assertLogs(logger, level) The with block logs on logger with minimum level assert_logs(logger, level)

There are also other methods used to perform more specific checks, such as:

Method In unitest Checks that Method In nose
assertAlmostEqual(a, b) round(a-b, 7) == 0 assert_almost_equal(a, b)
assertNotAlmostEqual(a, b) round(a-b, 7) != 0 assert_not_almost_equal(a, b)
assertGreater(a, b) a > b assert_greater(a, b)
assertGreaterEqual(a, b) a >= b assert_greater_equal(a, b)
assertLess(a, b) a < b assert_less(a, b)
assertLessEqual(a, b) a <= b assert_less_equal(a, b)
assertRegex(s, r) r.search(s) assert_regex(a, b)
assertNotRegex(s, r) not r.search(s) assert_not_regex(s, r)
assertCountEqual(a, b) a and b have the same elements in the same number, regardless of their order assert_count_equal(a, b)

The list of type-specific methods automatically used by assertEqual() are summarized in the following table. Note that it’s usually not necessary to invoke these methods directly:

Method In unitest Used to compare Method In nose
assertMultiLineEqual(a, b) strings assert_multi_line_equal(a, b)
assertSequenceEqual(a, b) sequences assert_sequence_equal(a, b)
assertListEqual(a, b) lists assert_list_equal(a, b)
assertTupleEqual(a, b) tuples assert_tuple_equal(a, b)
assertSetEqual(a, b) sets or frozensets assert_set_equal(a, b)
assertDictEqual(a, b) dicts assert_dict_equal(a, b)