pytest.fixture如何像testng的beforeMethod一样使用

熟悉使用testng的童鞋们应该都晓得beforeMethod,在测试脚本执行时,可以对每个测试函数进行初始化。
比如app的启动,浏览器的启动等,都可以定义到beforemethod里面。

/**
 * browser initialize
 * @throws Throwable
 * @author: 爱吃苹果的鱼   
 */
@BeforeMethod(groups = "all")
public void beforeMethod() throws Throwable{
	try {
		switch(testNGTestName.toLowerCase()) {
		case "h5":
		case "web":{
			driver = webDriverManager.getCurrentWebDriver(driverBrowserType);
			driver.manage().window().maximize();
			driver.manage().deleteAllCookies();
			break;
		}
		default:break;
		}
	} catch (Exception e) {
		logger.error("Exception happened: " + e.getMessage());
		throw new FrameworkException(e);
	}
}
/**
 * driver initialize
 * author:爱吃苹果的鱼
 * date:2019年12月27日
 */
@Parameters({"noReset", "port", "udid", "appPackage", "appActivity", "platformVersion"})
@BeforeMethod(groups = "all")
public void beforeMethod(String noReset, String port, String udid, 
		String appPackage, String appActivity, String platformVersion) throws Throwable{
	try {
		DesiredCapabilities cap = new DesiredCapabilities();
		cap.setCapability("unicodeKeyboard", true);//中文
		cap.setCapability("automationName", "UiAutomator2");
		cap.setCapability("noReset", noReset);
		cap.setCapability("platformName", "Android");
		cap.setCapability("platformVersion", platformVersion);
		cap.setCapability("udid", udid);
		cap.setCapability("deviceName", udid);
		cap.setCapability("appPackage", appPackage);
		cap.setCapability("appActivity", appActivity);			
		appHandler = new AndroidDriver<>(new URL("http://127.0.0.1:" + port + "/wd/hub"), cap);
		Thread.sleep(3000);
	} catch (Exception e) {
		logger.error("Exception happened: " + e.getMessage());
		throw new FrameworkException(e);
	}
}

那在pytest中如何实现呢?可以通过pytest.fixture(scope=‘function’)去实现。怎样实现呢?我们需要在python的测试脚本文件的开始定义一个setup函数,并加上pytest.fixture(scope=‘function’)这个标签,在具体的测试函数中,引用它即可

@pytest.fixture(scope='function')
def setup(self):
	'''页面初始化'''
    self.webInit = selenium_init.SeleniumInit()
    self.driver = self.webInit.setup('web')
    self.webAct = selenium_action.SeleniumActionAPI(self.driver)

@pytest.mark.web
@allure.story('test_story_of_web_demo')
def test_web_demo(self, setup):
	'''测试函数'''
    self.webAct.to_url("http://www.baidu.com")
    self.webAct.ele_send_keys(self.webAct.ele_get_by_id('kw'), 'selenium') # 传入搜索关键字
    self.webAct.ele_click_by_id('su')  # 点击百度一下按钮
    time.sleep(3)
    result_list = self.webAct.ele_list_get_by_xpath("//div[@class='result c-container ']/h3/a")
    result = False
    for i in range(len(result_list)):
        if 'selenium' in str(result_list[i].text).lower():
            result = True
        else:
            print(result_list[i].text + ':不包含selenium')
            result = False
    assert result

def teardown(self):
	'''页面退出'''
    self.driver.quit()
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值