擅长:python、mysql、java
<p>当然,这样做肯定会奏效:</p>
<pre><code>class BaseTestCase(unittest.TestCase):
def setUp(self):
pass # common teardown
def tearDown(self):
pass # common teardown
class ATestCase(BaseTestCase):
def test1(self):
pass
class BTestCase(BaseTestCase):
def test1(self):
pass
</code></pre>
<p>如果<code>ATestCase</code>或{<cd2>}的知识在<code>BaseTestCase</code>中需要,只需重写子类中的某个方法,但在超类中使用它。在</p>
^{pr2}$
<p>请注意,如果任何<code>test_(self)</code>方法将在BaseTestCase中实现,那么当自动运行程序发现它们时,它们将运行(并且由于安装失败而失败)。在</p>
<p>作为一种解决方法,您可以在抽象测试的setUp子句中使用<code>skipTest</code>,并在子类中重写它。在</p>
<pre><code>class BaseTestCase(unittest.TestCase):
def setUp(self):
self.instance = self._create_instance()
def _create_instance(self):
self.skipTest("Abstract")
def test_fromBase(self):
self.assertTrue(True)
</code></pre>
<p>请注意,跳过<code>test_fromBase</code>(例如,通过decorator)是不好的,因为“test should be skipped”逻辑将由所有子类继承。在</p>