Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This question was answered <a href="http://mail.python.org/pipermail/python-list/2012-October/632388.html">by Peter Otten on comp.lang.python</a>.</p> <p>Move MyTestCase in a separate module and define a global variable <code>__unittest = True</code>.</p> <pre class="lang-none prettyprint-override"><code>$ cat mytestcase.py import unittest __unittest = True class MyTestCase(unittest.TestCase): def assertLengthIsOne(self, sequence, msg=None): if len(sequence) != 1: msg = self._formatMessage(msg, "length is not one") raise self.failureException(msg) $ cat mytestcase_demo.py import unittest from mytestcase import MyTestCase class TestFoo(MyTestCase): seq = (1, 2, 3, 4, 5) def test_stock_unittest_assertion(self): self.assertEqual(len(self.seq), 1) def test_custom_assertion(self): self.assertLengthIsOne(self.seq) if __name__ == "__main__": unittest.main() $ python mytestcase_demo.py FF ====================================================================== FAIL: test_custom_assertion (__main__.TestFoo) ---------------------------------------------------------------------- Traceback (most recent call last): File "mytestcase_demo.py", line 11, in test_custom_assertion self.assertLengthIsOne(self.seq) AssertionError: length is not one ====================================================================== FAIL: test_stock_unittest_assertion (__main__.TestFoo) ---------------------------------------------------------------------- Traceback (most recent call last): File "mytestcase_demo.py", line 8, in test_stock_unittest_assertion self.assertEqual(len(self.seq), 1) AssertionError: 5 != 1 ---------------------------------------------------------------------- Ran 2 tests in 0.000s FAILED (failures=2) $ </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload