作者jacobcan118 (jacobcan118)
看板Python
标题[问题]在不同测试case中共用同一个变数-pytest
时间Wed Oct 19 12:35:21 2016
请问我想用pytest写单元测试.不过如果我在每个测试中都需要用selenium开启页面.我有任何方法可以使每个测试共用base这个变数吗
谢谢
class TestLogin(object):
def setup_method(self, method):
self.driver = WebDriver(desired_capabilities=desired_capabilities, command_executor=command_executor)
self.current_method_name = method.__name__
def teardown_method(self, method):
self.driver.close()
self.driver.quit()
@pytest.fixture(scope="function")
def loadpage():
self.base = Home(self.driver).open()
def loadLogin():
base.loadLogin()
def test_a(self):
base = Home(self.driver).open()
assert True == base.dotesta()
base.loadLogin()
def test_b(self):
base = Home(self.driver).open()
assert True == base.dotestb()
base.loadLogin()
def test_c(self):
base = Home(self.driver).open()
assert True == base.dotestc()
base.loadLogin()
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 108.6.244.145
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1476851724.A.9A1.html
1F:→ Typebrook: 用 global 前缀看看 10/19 16:50
2F:推 aweimeow: 我之前是在 class 外写 fixture 10/20 10:28
3F:→ aweimeow: @pytest.fixture(scope="module") 10/20 10:29
4F:→ aweimeow: def base(): 10/20 10:29
5F:→ aweimeow: return Home(self.driver).open() 10/20 10:29
6F:→ aweimeow: 然後在 class 那边加上你要用的 fixture 10/20 10:29
7F:→ aweimeow: @pytest.mark.usefixtures('base') 10/20 10:30
8F:→ aweimeow: class TestLogin(object): 10/20 10:30
9F:→ aweimeow: def loadpage(self, base): 10/20 10:30
10F:→ aweimeow: 你参考看看 XD 10/20 10:31