获取测试用例的执行状态,可以知道每个测试用例执行成功还是失败,或者是跳过,有了这些信息,我们可以做:

  1. 自己定制测试报告
  2. 当用例失败的时候截图。

参考 pytest 官网文档 https://docs.pytest.org/en/6.2.x/example/simple.html#making-test-result-information-available-in-fixtures

定义conftest.py 文件

编写conftest.py 文件

  1. import pytest
  2. @pytest.hookimpl(tryfirst=True, hookwrapper=True)
  3. def pytest_runtest_makereport(item, call):
  4. # execute all other hooks to obtain the report object
  5. outcome = yield
  6. # 获取用例的执行结果
  7. rep = outcome.get_result()
  8. # 将执行结果保存到 item 属性中 req.when 执行时
  9. setattr(item, "rep_" + rep.when, rep)
  10. # 默认是function级别 执行测试用例的时候会自动调用
  11. @pytest.fixture(scope='function',autouse=True)
  12. def something(request):
  13. yield
  14. # 当用例执行失败的操作:
  15. if request.node.rep_call.failed:
  16. print("用例执行失败", request.node.nodeid)

编写测试用例文件
自动化测试用例文件不受影响,和原来的写法一致

  1. def test_a():
  2. assert 1==2
  3. def test_b():
  4. assert 2==2

运行测试用例文件时候, 可以拿到结果。

app自动化错误时截图

根据上面的代码,来实现app执行失败时截图操作。

conftest.py 添加代码

  1. from appium import webdriver
  2. import pytest
  3. import os
  4. from appium.webdriver.webdriver import WebDriver
  5. chromedriver= os.path.join(os.path.dirname(os.path.abspath(__file__)),'drivers/chromedriver.exe')
  6. @pytest.fixture(scope='session',autouse=True)
  7. def driver():
  8. desired_caps = {
  9. 'platformName': 'Android', # 测试Android系统
  10. 'platformVersion': '7.1.2', # Android版本 可以在手机的设置中关于手机查看
  11. 'deviceName': '127.0.0.1:62001', # adb devices 命令查看 设置为自己的设备
  12. 'automationName': 'UiAutomator2', # 自动化引擎
  13. 'noReset': False, # 不要重置app的状态
  14. 'fullReset': False, # 不要清理app的缓存数据
  15. 'chromedriverExecutable': chromedriver, # chromedriver 对应的绝对路径
  16. 'appPackage': "org.cnodejs.android.md", # 应用的包名
  17. 'appActivity': ".ui.activity.LaunchActivity" # 应用的活动页名称
  18. }
  19. driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_capabilities=desired_caps)
  20. driver.implicitly_wait(5) # 全局的隐式等待时间
  21. yield driver # 将driver 传递出来
  22. driver.quit()
  23. @pytest.hookimpl(tryfirst=True, hookwrapper=True)
  24. def pytest_runtest_makereport(item, call):
  25. # execute all other hooks to obtain the report object
  26. outcome = yield
  27. # 获取用例的执行结果
  28. rep = outcome.get_result()
  29. # 将执行结果保存到 item 属性中 req.when 执行时
  30. setattr(item, "rep_" + rep.when, rep)
  31. @pytest.fixture(scope='function',autouse=True)
  32. def case_run(driver:webdriver,request):
  33. """
  34. 每个测试用例执行完成之后,如果执行失败截图,截图的名称为测试用例名称+时间格式
  35. :param request:
  36. :return:
  37. """
  38. yield
  39. if request.node.rep_call.failed:
  40. import os,time
  41. screenshots = os.path.join(os.path.dirname(os.path.abspath(__file__)),'screeshots')
  42. if not os.path.exists(screenshots):
  43. os.mkdir(screenshots)
  44. casename:str = request.node.nodeid
  45. print("执行测试用例的名字:",casename)
  46. # 测试用例的名字
  47. # casename = casename.replace('.py::','_')
  48. filename = time.strftime('%Y_%m_%d_%H_%M_%S')+".png"
  49. screenshot_file = os.path.join(screenshots,filename)
  50. # 保存截图
  51. driver.save_screenshot(screenshot_file)

image.png