如何使用Python unittest模块构建我的测试?

我正在尝试为selenium和unittest中的自动化webtesting构建一个测试框架,我想将我的测试结构化为不同的脚本.所以我把它组织如下:

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运行的非常特殊的方式,我一定错过了关于文档如何期望我构建测试的技巧.有什么指针吗?

解决方法:

不是100%肯定,但在main.py中,您可能需要:

SeTestSuite = unittest.defaultTestLoader.discover(start_dir='.')

跑步者应该是(也许):

# if your line didn't work
unittest.TextTestRunner(verbosity=2).run(unittest.TestSuite(SeTestSuite))

相关文章

转载地址:https://www.cnblogs.com/mini-monkey/p/12104821...
web自动化测试过程中页面截图相对比较简单,可以直接使用sel...
目录前言一、Selenium简介二、浏览器驱动1.浏览器驱动参考2....
一、iframe的含义:iframe是HTML中框架的一种形式,在对界面...
转载请注明出处❤️作者:测试蔡坨坨原文链接:caituotuo.to...
'''##**认识selenium**​**下载:pipinstall...