我有一些使用 Nose 运行的 Python 测试。一个例子是这样的:
class Logout(unittest.TestCase):
def report_pass_fail(self):
#code
def setUp(self):
#code
def test_Logout(self):
#code
def tearDown(self):
#code
因为我有大量的测试并且因为我想创建一个模块化的代码架构......我想在一个单独的文件(模块)中拥有 report_pass_fail 、 setUp 和 tearDown 方法,并在需要时在内部调用它们考试的课。
我在 OOP 方面不是很有经验。我尝试了几种组合但没有成功。我怎么能那样做?
我创建的模块是这样的:
import json, httplib, base64, unittest, sys
from selenium import webdriver
import className
from creds import config, sauce_hub
class Fixtures(unittest.TestCase):
def report_pass_fail(self):
base64string = base64.encodestring('%s:%s' % (config['username'], config['access-key']))[:-1]
result = json.dumps({'public': 'true', 'passed': sys.exc_info() == (None, None, None)})
connection = httplib.HTTPConnection("saucelabs.com")
connection.request('PUT', '/rest/v1/%s/jobs/%s' % (config['username'], self.wd.session_id), result, headers={"Authorization": "Basic %s" % base64string})
result = connection.getresponse()
return result.status == 200
def setUp(self):
desired_capabilities = webdriver.DesiredCapabilities.FIREFOX
desired_capabilities['version'] = '4'
desired_capabilities['platform'] = 'Linux'
desired_capabilities['name'] = className.getName(self)
desired_capabilities['record-video'] = False
self.wd = webdriver.Remote(desired_capabilities=desired_capabilities,
command_executor="http://" + config['username'] + ":" + config['access-key'] + sauce_hub)
self.wd.implicitly_wait(10)
def tearDown(self):
self.wd.quit()
它在测试文件中的使用是这样的:
import unittest
from fixture_module import Fixtures
#is_alert_present(wd)
#credentials
class DeleteLectureFromCoursePanel(Fixtures,unittest.TestCase):
import fixture_module
f = fixture_module.Fixtures()
f.report_pass_fail()
f.setUp()
def test_DeleteLectureFromCoursePanel(self):
success = True
wd = self.wd
wd.find_element_by_link_text("Delete").click()
self.assertEqual("Are you sure?", wd.switch_to_alert().text)
wd.switch_to_alert().accept()
self.assertTrue(success)
f.tearDown()
if __name__ == '__main__':
unittest.main()
看起来没问题,PyCharmIDE 中没有错误,但是当我从控制台运行它时,它说:
ValueError:no such test method in <class 'fixture_module.Fixtures'> : runTest