【发布时间】:2012-09-04 14:18:39
【问题描述】:
我正在尝试为 Selenium 和 unittest 中的自动化 Web 测试构建一个测试框架,并且我想将我的测试构建成不同的脚本。
所以我整理如下:
文件 base.py - 目前将包含用于设置会话的基本 Selenium 测试用例类。
import unittest
from selenium import webdriver
# Base Selenium Test class from which all test cases inherit.
class BaseSeleniumTest(unittest.TestCase):
def setUp(self):
self.browser = webdriver.Firefox()
def tearDown(self):
self.browser.close()
文件 main.py - 我希望这是运行所有单独测试的整体测试套件。
import unittest
import test_example
if __name__ == "__main__":
SeTestSuite = test_example.TitleSpelling()
unittest.TextTestRunner(verbosity=2).run(SeTestSuite)
文件 test_example.py - 一个示例测试用例。让它们自己运行可能会很好。
from base import BaseSeleniumTest
# Test the spelling of the title
class TitleSpelling(BaseSeleniumTest):
def test_a(self):
self.assertTrue(False)
def test_b(self):
self.assertTrue(True)
问题是当我运行 main.py 时,我得到以下错误:
Traceback (most recent call last):
File "H:\Python\testframework\main.py", line 5, in <module>
SeTestSuite = test_example.TitleSpelling()
File "C:\Python27\lib\unittest\case.py", line 191, in __init__
(self.__class__, methodName))
ValueError: no such test method in <class 'test_example.TitleSpelling'>: runTest
我怀疑这是由于 unittest 运行的非常特殊的方式,我一定错过了文档希望我如何构建测试的技巧。有什么指点吗?
【问题讨论】:
-
+1 我很好奇为什么它不起作用,因为它看起来大部分正确。根据您的“接受”,我想我的建议有效吗?
-
@aneroid 是的,抱歉 -
SeTestSuite = test_example.TitleSpelling()行错了,我误解了文档。最好的办法是将所有测试放在一个单独的文件夹中,并使用您在下面建议的 discover() 方法,它将通过该目录递归并运行其中的所有测试!
标签: python unit-testing selenium