问题描述
我正在尝试在Linux Ubuntu环境中的Python中运行Selenium测试。 Geckodriver位于我的项目根文件夹中。 我从PyCharm命令行运行名为siteTest.py的文件:
python3 siteTest.py
但是,我看不到Selenium的任何输出。 在将测试分为setUp,test和tearDown并添加了 self 作为参数之前,该测试有效。 有什么建议我做错了吗? 预先感谢。
import os
import unittest
from selenium import webdriver
class siteTest:
def setUp(self):
ROOT_DIR = os.path.abspath(os.curdir)
self.driver = webdriver.Firefox(executable_path=ROOT_DIR + '/geckodriver')
def test(self):
driver = self.driver
driver.get('https://google.com/')
def tearDown(self):
self.driver.quit()
if __name__ == "__main__":
unittest.main()
解决方法
您的程序接近完美。您只需要将siteTest
class 注释为unittest.TestCase
。如此有效,您需要重写以下行:
class siteTest:
为:
class siteTest(unittest.TestCase):
,
您可能需要注释设置和拆卸方法。
@classmethod
def setUp(self)
.
.
@classmethod
def tearDown(self)
.
.
在这里,我已注释为类方法,因此它将仅在该类中运行一次。