yield可以返回多个值到setup函数中去,但是需要用括号括起来,然后下面具体的函数接受到传值就不需要每次都实例化了。
举例如下:
@pytest.fixture()
def setup(driver): nav_bar = NavBar(driver) vir_kb = Virtual_Keyboard(driver) if not nav_bar.is_left_nav_bar_open(): nav_bar.click_menu_bar() session_list = SessionListPage(driver) session_list.launch_session(1) api = BzwApi(driver) api.init_obj() sleep(2) yield (api, nav_bar, vir_kb) #需要用括号括起来 api.close_session()@pytest.mark.usefixtures("create_mf_session")class Test3270OtherFunctionKeysInVirtualKeyboard(): """Test TE-BlueZone Web 08_3270 other Function Keys in Virtual Keyboard""" def test_3270_function_key_backspace(self, setup): #接受到传值就不需要每次都实例化了,这里是setup, api, nav_bar, vir_kb = setup #这里就是实例化 api.send_text('logon') cursor_col1 = api.get_cursor()[0] cursor_row1 = api.get_cursor()[1] nav_bar.click_virtual_keyboard() vir_kb.click_bksp() sleep(1) cursor_col2 = api.get_cursor()[0] cursor_row2 = api.get_cursor()[1] assert (cursor_col1 == cursor_col2 + 1) and (cursor_row1 == cursor_row2)PS:需要注意的是,yield返回几个值,实例化就要用几个值,否则就会报错。这里一般是指返回的多,实例化的少,也是不行的。