Wol*_*tan 11 python eclipse unit-testing eclipse-plugin pydev
我使用PyDev单元测试在eclipse中对我的python代码进行单元测试.我右键单击相应的文件并选择Run As - > Python unit-test.关于这个插件,我有几个问题:
在此先感谢任何回答和评论^^
Cherio Woltan
例:
class TestClass(unittest.TestCase):
@classmethod
def setUpClass(self):
print "Setup"
def test1(self):
print "Test1"
def test2(self):
print "Test2"
Run Code Online (Sandbox Code Playgroud)
如果我运行这与运行方式- >的Python单元测试的setUpClass方法不被调用.
这是一个PyDev错误,已在2.0.1中修复.
setUpModule()
,tearDownModule()
,setUpClass()
,和tearDownClass()
未在运行配置中的"Python的单元测试"由于PyDev的2.0.0和更早的bug运行.在2.0.1中,它们在"Python单元测试"和"Python运行"配置中正确运行.我自己测试一下来验证.
好吧,我会给这个镜头:我使用Pydev并一直在探索使用"nosetests"来启动测试,所以它有点相关.我的解决方案是完全破解,但在PyDev中说"Debug as UnitTest"时似乎确实有效:
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..."
Run Code Online (Sandbox Code Playgroud)
不幸的是,这在使用nosetests时不起作用,但从pydev运行时它确实有效.我猜测nosetests被编码为在每个测试方法运行之前实例化测试对象,在这种情况下你的init方法就足够了.
关于测试,我不能说足够多的好东西:
例子:
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
Run Code Online (Sandbox Code Playgroud)
如果您正在使用nosetests进一步调查,那么它已经涵盖了您,请参阅"http://somethingaboutorange.com/mrl/projects/nose/1.0.0/writing_tests.html".
你可以有:
包级别拆解:(这些存在于包级别的init脚本中)
def setup_package()
def teardown_package()
Run Code Online (Sandbox Code Playgroud)
模块级拆解:
def setup_module()
def teardown_module()
Run Code Online (Sandbox Code Playgroud)
班级:
class MyTestCase(unittest.TestCase):
@classmethod
def setup_class(cls): pass
@classmethod
def teardown_class(cls): pass
Run Code Online (Sandbox Code Playgroud)
和测试方法级别:
class MyTestCase(unittest.TestCase):
def setUp(self): pass
def tearDown(cls): pass
Run Code Online (Sandbox Code Playgroud)
我喜欢使用"setup_"名称,因为它很好地描绘了鼻子特定的入口点.通过命令行从nosetests运行时,我已经很好地验证了这些工作.但他们并没有从Pydev运行"作为单元测试运行......".一个潜在的解决方案可能是编写一个使用nose运行测试的pydev插件......也许有人有一个?您可以将我的hack与调用常用模块函数的鼻子结合起来进行实际工作.理想情况下,我们的init()会意识到以某种方式从Pydev启动.