加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-20 13:30:04 所属栏目:Python 来源:网络整理
导读:我正在尝试为selenium和unittest中的自动化webtesting构建一个测试框架,我想将我的测试结构化为不同的脚本.所以我把它组织如下: base.py – 目前,这将包含用于设置会话的基本selenium测试用例类. import unittestfrom selenium import webdriver# Base Sele
我正在尝试为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:Pythontestframeworkmain.py",line 5,in <module>
    SeTestSuite = test_example.TitleSpelling()
  File "C:Python27libunittestcase.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))

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读