Domanda

ho un figlio unittest.TestCase, che definisce diversi domini controlli specifici assert. Vorrei eseguire alcuni Unittests a unittest questa funzionalità per controllare che il lavoro tutto in aggiornamenti futuri.

class MyBaseTestCase(unittest.TestCase):
    def setUp(self):
        ...
    def tearDown(self):
        ...
    def run(self, result):
        ...
    def assertSpec(self, condition, message):
        ...

Voglio MyBaseTestCase instantiate in altri test di unità, in questo modo:

class TestBase(unittest.TestCase):
    def test_assertSpec(self):
        self.testclass = MyBaseTestCase()
        self.assertRaises(AssertionError, self.testclass.assertSpec, False)

Sto incontrando errori multipli diversi, quando si fa questo, quindi la mia domanda è: che cosa è una pratica comune per unit testing personalizzato funzioni unittest?

In caso aiuta:

$ jython
>>> import sys
>>> sys.version
'2.5.2 (Release_2_5_2:7206, Mar 2 2011, 23:12:06) \n[Java HotSpot(TM) Server VM (Sun Microsystems Inc.)]'
È stato utile?

Soluzione

Non so se c'è qualcosa di specifico che Jython romperebbe questo, ma vorrei fare qualcosa di simile:

import StringIO
import unittest


# The TestCase subclass to test    
class MyBaseTestCase(unittest.TestCase):
    def assertSpec(self, thing):
        assert thing == 123


# The testcase for MyBaseTestCase
class TestMyTest(unittest.TestCase):
    def test_assetSpec(self):
        """Ensure assertSpec works
        """

        class _TestSpec(MyBaseTestCase):
            def test_failure_case(self):
                 self.assertSpec(121)
            def test_success_case(self):
                 self.assertSpec(123)

        # Load tests from _TestSpec
        loader = unittest.TestLoader()
        suite = loader.loadTestsFromTestCase(_TestSpec)

        # Create runner, and run _TestSpec
        io = StringIO.StringIO()
        runner = unittest.TextTestRunner(stream = io)
        results = runner.run(suite)

        # Should be one failed test, and one passed test
        self.assertEquals(results.testsRun, 2)
        self.assertEquals(len(results.failures), 1)

if __name__ == "__main__":
    unittest.main()
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top