pytest加载测试用例的一个问题:A session is either terminated or not started
一. 引出问题
为了测试一种情况,我在目录TestCases下新建了一个test_login.py和test_welcome.py,然后发现在模块里面各自运行时,都是正常的,在命令行运行时,后者可以正常,前者总是出错:
二. 测试代码分析
先看看两个模块的代码是怎么设计的
test_login.py
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from appium import webdriver from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from appium.webdriver.common.mobileby import MobileBy import time, pytest import logging from Common.my_logging import * #操作对象的设备信息 desired_caps = {} desired_caps["platformName"] = "Android" desired_caps["platformVersion"] = "5.1.1" desired_caps["deviceName"] = "Android Emulator" desired_caps["appPackage"] = "com.xxzb.fenwoo" desired_caps["appActivity"] = "com.xxzb.fenwoo.activity.addition.WelcomeActivity" #连接appium driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) time.sleep(3) logging.info("加载test_login") logging.info("test_login_driver是: " + str(driver)) @pytest.mark.login def test_login_success(): logging.info("test_login开始了!") logging.info("test_login的driver是: " + str(driver)) size = driver.get_window_size() for i in range(3): driver.swipe(size["width"] * 0.9, size["height"] * 0.5, size["width"] * 0.1, size["height"] * 0.5, 200) WebDriverWait(driver, 10, 1).until(EC.visibility_of_element_located((MobileBy.ID, "com.xxzb.fenwoo:id/btn_start"))) driver.find_element_by_id("com.xxzb.fenwoo:id/btn_start").click() WebDriverWait(driver, 10, 1).until(EC.visibility_of_element_located((MobileBy.ID, "com.xxzb.fenwoo:id/btn_login"))) driver.find_element_by_id("com.xxzb.fenwoo:id/btn_login").click() WebDriverWait(driver, 10, 1).until(EC.visibility_of_element_located((MobileBy.ID, "com.xxzb.fenwoo:id/et_phone"))) driver.find_element_by_id("com.xxzb.fenwoo:id/et_phone").send_keys("18684720553") driver.find_element_by_id("com.xxzb.fenwoo:id/btn_next_step").click() WebDriverWait(driver, 10, 1).until(EC.visibility_of_element_located((MobileBy.ID, "com.xxzb.fenwoo:id/et_pwd"))) driver.find_element_by_id("com.xxzb.fenwoo:id/et_pwd").send_keys("python") driver.find_element_by_id("com.xxzb.fenwoo:id/btn_next_step").click() assert True == True
test_welcome.py
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from appium import webdriver from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from appium.webdriver.common.mobileby import MobileBy import time import pytest import logging from Common.my_logging import * #操作对象的设备信息 desired_caps = {} desired_caps["platformName"] = "Android" desired_caps["platformVersion"] = "5.1.1" desired_caps["deviceName"] = "Android Emulator" desired_caps["appPackage"] = "com.xxzb.fenwoo" desired_caps["appActivity"] = "com.xxzb.fenwoo.activity.addition.WelcomeActivity" #连接appium driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) time.sleep(3) logging.info("加载test_welcome") logging.info("test_welcome_driver是: " + str(driver)) #滑屏操作 @pytest.mark.welcome def test_welcome(): logging.info("test_welcome开始了!") logging.info("test_welcome的driver是: " + string(driver)) size = driver.get_window_size() for i in range(3): driver.swipe(size["width"] * 0.9, size["height"] * 0.5, size["width"] * 0.1, size["height"] * 0.5, 200) WebDriverWait(driver, 10, 1).until(EC.visibility_of_element_located((MobileBy.ID, "com.xxzb.fenwoo:id/btn_start"))) driver.find_element_by_id("com.xxzb.fenwoo:id/btn_start").click() driver.close_app() driver.quit() assert True == True
可以发现,两个模块中都定义了driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps),而在pytest收集测试用例的时候,有一个顺序:
1. 先收集以test_*.py或*_test.py形式的模块,pytest在收集的时候是按照字母a-z,数字0-9的顺序
2. 然后收集类之外的以test开头的测试方法
3. 最后收集Test开头的类里面的test开头的测试方法
三. 分析
简单的画了一个图,大概说明下pytest收集用例的顺序,先收集test_login.py时会创建一个session为00b7be的session对象,然后收集test_welcome.py时会创建一个session为187161的session对象,由于是同一个appium server,端口都为4723,自然后面的对象建立后,前面的会断开。所以测试方法test_login_success运行时,会提示"A session is either terminated or not started"
从日志里也可以看到这点
Tue, 14 Apr 2020 16:21:04 test_login.py <module> [line:21] INFO 加载test_login Tue, 14 Apr 2020 16:21:04 test_login.py <module> [line:22] INFO test_login_driver是: <appium.webdriver.webdriver.WebDriver (session="00b7bec7-8d8c-4579-8fdb-322abcf4c83c")> Tue, 14 Apr 2020 16:21:15 test_welcome.py <module> [line:23] INFO 加载test_welcome Tue, 14 Apr 2020 16:21:15 test_welcome.py <module> [line:24] INFO test_welcome_driver是: <appium.webdriver.webdriver.WebDriver (session="1871614e-6cdf-443c-b6e0-fe9b3175839d")> Tue, 14 Apr 2020 16:21:15 test_login.py test_login_success [line:28] INFO test_login开始了! Tue, 14 Apr 2020 16:21:15 test_login.py test_login_success [line:29] INFO test_login的driver是: <appium.webdriver.webdriver.WebDriver (session="00b7bec7-8d8c-4579-8fdb-322abcf4c83c")>
一种修改的办法是,在两个模块的方法中,初始化driver对象,这样执行测试方法时,能保证自己的driver是一个在线的状态。但这种方法不太合理,因为一个方法实例化一个driver对象,那么N个方法就要初始化N个driver对象,显然是不合适的。所以有必要将公共的driver提取出来,保证一个driver能贯穿整个测试流程