<p>好吧,我来试一试:我使用Pydev,并且一直在探索使用“nosetests”来启动测试,所以它有点相关。我的解决方案是彻底的破解,但是当从PyDev内部说“Debug as UnitTest”时似乎确实有效:</p>
<pre><code>print "before any tests (global) just once..."
class MyTestCase(unittest.TestCase):
class_setup_called = False
def __init__(self, test_name):
unittest.TestCase.__init__(self, test_name)
if not self.__class__.class_setup_called:
self.setUpClass()
self.__class__.class_setup_called = True
@staticmethod
def setUpClass():
print "before first test only..."
def setUp(self):
print "before each test..."
</code></pre>
<p>不幸的是,这在使用nosetest时不起作用,但在从pydev运行时确实起作用。我猜nosetests是在运行每个测试方法之前被编码来实例化测试对象的,在这种情况下,init方法就足够了。</p>
<p>我说不出鼻子测试的好话:</p>
<ul>
<li>支持异常和定时
测验。</li>
<li>支持注释“属性”。</li>
<li>集成概要文件、覆盖范围和结果的xunit报告。</li>
<li>测试用例的递归发现和属性执行。</li>
</ul>
<p>示例:</p>
<pre><code>import unittest
@raises(TypeError)
def test_forexceptions(self):
pass
@attr('benchmark')
@timed(5.0)
def test_benchmark(self):
pass # do something real slow and run nosetests --with-profile -a benchmark
</code></pre>
<h3>附加</h3>
<p>在进一步的调查中,如果你正在使用nose tests,那么它已经覆盖了你,请参见“http://somethingaboutorange.com/mrl/projects/nose/1.0.0/writing_tests.html”。</p>
<p>你可以有:</p>
<p>包级拆分:(这些位于包级init脚本中)</p>
<pre><code>def setup_package()
def teardown_package()
</code></pre>
<p>模块级拆卸:</p>
<pre><code>def setup_module()
def teardown_module()
</code></pre>
<p>班级级别:</p>
<pre><code>class MyTestCase(unittest.TestCase):
@classmethod
def setup_class(cls): pass
@classmethod
def teardown_class(cls): pass
</code></pre>
<p>试验方法水平:</p>
<pre><code>class MyTestCase(unittest.TestCase):
def setUp(self): pass
def tearDown(cls): pass
</code></pre>
<p>我喜欢使用“setup”名称,因为它很好地描绘了特定于鼻子的入口点。当通过命令行从nosetests运行时,我已经很好地验证了这些功能。但它们不是从Pydev“作为单元测试运行…”运行的。一个潜在的解决方案可能是编写一个pydev插件,该插件使用nose运行测试。。。也许有人有?你可以把我的黑客技术和nose技术结合起来,调用公共模块函数来完成实际的工作。理想情况下,我们的<strong>init</strong>(初始化)会意识到以某种方式从Pydev启动。</p>