我有我希望成爲一個非常普遍的問題。我正在測試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