我有我希望成为一个非常普遍的问题。我正在测试XML文件并验证特定的标签。我不想重写每个字段的代码,所以我试图重用标签检查代码。但是,当我尝试这样做时如何重用python测试用例类
这里是我尝试使用UnitTest框架的“ValidateField”类。这是我正在尝试使用的代码。我只是试图按以下方式调用它。我得到的是单元测试框架运行0次测试。我在我的程序的其他地方成功地使用了单元测试框架,我没有试图重用一个类。任何人现在都会误入歧途?
这里是输出我得到:
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
----------------------------------------------------------------------
这里是类/方法我在哪里是可重用的类使用使得调用可重用的类
import Common.ValidateField
class Tests():
def test_testCreated(self):
validate = Common.ValidateField.ValidateField()
validate.test_field('./created')
这里单元测试框架。
import unittest
import Main
import Common.Logger
class ValidateField(unittest.TestCase):
def test_field(self, xpath):
driver = Main.ValidateDriver().driver
logger = Common.Logger.Logger
tag = []
for t in driver.findall("'" + xpath + "'"):
tag.append(t)
# Test to see if there is more than one tag.
if len(tag) > 1:
self.assertTrue(False, logger.failed("There is more than one " + "'" + t.text + "'" + " tag."))
else:
# Test for a missing tag.
if len(tag) <= 0:
self.assertTrue(False, logger.failed("There is no " + "'" + t.text + "'" + " tag."))
# Found the correct number of tags.
self.assertTrue(True, logger.passed("There is only one " + "'" + t.text + "'" + " tag."))
# Test to make sure there is actually something in the tag.
if t.text is None:
self.assertTrue(False, logger.failed("There is a " + "'" + t.text + "'" + " tag, but no value exists"))
你是如何运行的(或者,在这种情况下......没有运行)测试? – geoffspear