簡體   English   中英

如何使用 Python 'unittest' 模塊構建我的測試?

[英]How do I structure my tests with the Python 'unittest' module?

我正在嘗試在 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運行的非常特殊的方式,我一定錯過了文檔希望我如何構建測試的技巧。 任何指針?

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

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

亞軍線應該是(也許):

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM