我设法通过拥有一个我的测试将子类化的基类来使其工作。该基类将创建一个静态驱动程序,该驱动程序将由 setUp() 方法设置一次并返回到后续测试。
from selenium import webdriver
import unittest, time, re
class TestBase(unittest.TestCase):
driver = None
rand = str(random.uniform(1,10))
base_url = "desiredtestURLhere"
def setUp(self):
if (TestBase.driver==None):
TestBase.driver = webdriver.Firefox()
TestBase.driver.implicitly_wait(30)
return TestBase.driver
然后我跑了两个测试...
import unittest, time, re
from testbase import TestBase
class Login(TestBase):
def test_login(self):
driver = TestBase.driver
base_url = TestBase.base_url
driver.get(base_url)
# etc
测试 #2 将连续运行...
import random
import unittest, time, re
from testbase import TestBase
class CreateThread(TestBase):
def test_create_thread(self):
driver = TestBase.driver
base_url = TestBase.base_url
rand = TestBase.rand
driver.get(base_url + "/forum.php")
# etc
我的 testsuite.py 我会运行...
import unittest, sys
# The following imports are my test cases
import login
import create_thread
def suite():
tsuite = unittest.TestSuite()
tsuite.addTest(unittest.makeSuite(login.Login))
tsuite.addTest(unittest.makeSuite(create_thread.CreateThread))
return tsuite
if __name__ == "__main__":
result = unittest.TextTestRunner(verbosity=2).run(suite())
sys.exit(not result.wasSuccessful())
这是我第一次接触 Python,所以如果有任何明显的问题,我会很感激反馈。