【问题标题】:selenium test not opening a browser django硒测试没有打开浏览器django
【发布时间】:2015-04-06 05:20:41
【问题描述】:

我正在尝试运行硒测试,但这似乎没有发生任何事情。我测试了以下代码以确保设置正常工作:firefox 浏览器按预期加载。

在functional_tests.py中

browser = webdriver.Firefox()
broswer.get('http://localhost:8000')

但是当我把它改成如下:

import unittest
from selenium import webdriver

class NewVistorTest(unittest.TestCase):

    def setUp(self): 
        self.browser = webdriver.Firefox()

    def tearDown(self):
        self.browser.quit()

    def test_can_open_browser(self):
        self.browser.get('http://localhost:8000')
        self.assertIn('Test', self.browser.title)

它没有打开浏览器,什么也没有发生。我运行了这个 pythonfunctional_tests.py

组织单元测试和硒测试的最佳方式是什么。我想按模块名称运行它,而不是全部在 tests.py 或 test_abc.py 中,而不是按鼻子。

【问题讨论】:

    标签: python unit-testing selenium


    【解决方案1】:

    您希望它如何运行? Python 不会自动运行测试,只是因为它们在一个类中。您需要在文件末尾添加几行:

    class NewVistorTest(unittest.TestCase):
        ...
    
    if __name__ == '__main__':
        unittest.main(warnings='ignore')
    

    此条件是 Python 脚本如何检查它是否已从命令行执行,而不是仅由另一个脚本导入。我们调用unittest.main() 来启动unittest 测试运行器,它会自动在文件中找到测试和方法并运行它们。

    如您所见,没有该块,什么都不会发生。

    【讨论】:

    • 谢谢。是的,我错过了那部分。
    猜你喜欢
    • 2014-11-24
    • 1970-01-01
    • 1970-01-01
    • 2021-10-02
    • 2018-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多