Python 2.6: proper usage of unittest.TestSuite

后端 未结 3 1911
刺人心
刺人心 2020-12-21 08:12

Using Python 2.6, I have a very simple test in a python file in a directory:

#mytest.py
import unittest

class BasicTests(unittest.TestCase):
    def test_ok         


        
3条回答
  •  离开以前
    2020-12-21 08:56

    I don't usually work with unittest on the command line, but have my own test running scripts.

    You need to add a function suite to the module

    def suite():
        return unittest.TestLoader().loadTestsFromTestCase(BasicTests)
    

    and then call it like python -m unittest mytest.suite. But then I run into the following problem:

    TypeError: calling  returned ]>, not a test
    

    which happens because unittest uses something like isinstance(mytest.suite(), TestSuite) but through executing with -m, you get two different versions of the TestSuite class (one is __main__.TestSuite, the other is unittest.TestSuite), so isinstance returns false.
    To me, this looks like a bug. Patching unittest.py by inserting from unittest import TestSuite, TestCase at the beginning of loadTestsFromName solves the isinstance problem. Sorry I can't give you the "correct" solution (if there is one).

提交回复
热议问题