python自动化 - pytest

1. 导入 pytest

  1) pip install pytest

  2) file -> setting -> Python Interpreter

2. pytest 和 unittest的区别:

  1. 表达式区别

    unittest: 定义一个类, 集成 unittest.TestCase 

      class TestLogin(unittest.TestCase):
    pytest:  运行时选择 pytest运行

      def test_aaa():
          print("aaaaaaa")

  2. 断言的表达式

    unittest: sef.assertxxxx()

    pytest: assert 表达式(结果为 True- 断言成功,否则-断言失败!) 

      assert "aaaa" == "bbbb" # 断言失败

  3. 收集用例:
    unittest: 

    # 加载测试对象
    testloader = unittest.TestLoader()
    # 定义要测试的脚本
    suiter = testloader.discover(config.case_path)
    # 定义测试报告路径
    report_path = os.path.join(config.case_report)

    pytest: 自动收集用例

      收集机制:

      1、 目录: pytest命令从哪个目录下执行,就从哪个目录下开始搜索用例。

                      2、 文件: 搜索 test_xxxxx开头的文件

  4、fixture: 前置后置。

      unittest: setup& teardowm

      pytest: function  -> class - > Module -> Session

        

定义: 1)用例不需要使用fixture返回值: @pytest.usefixture("")
    2) 需要使用fixture的返回值:
      1. @pytest.mark.usefixture("xxx")  # xxx为函数名称 @pytest.mark.usefixtures("init")
      2. 函数名称作为用例参数  #def test_bb(self, init)
@pytest.fixture(scope="module")
def init():
    print("前置条件")
    driver = webdriver.Chrome()
    yield driver,true
    print("后置条件")
@pytest.fixture(scope="class")
def mycc():
    print("类级别的前置条件  开始")
    yield
    print("类级别的后置条件  结束")
@pytest.mark.usefixtures("mycc")
class TestAA:
    def test_aa(self):
        print("******************  test_aa ***********************")
    @pytest.mark.usefixtures("init")
    def test_bb(self, init): #(driver,true) 如果fixture有两个返回值,则以列表的形式显示,用下标取出
        init[0].get("https://www.baidu.com")
        print("******************  test_bb ***********************")
    def test_cc(self):
        print("******************  test_cc ***********************")
#输出结果
类级别的前置条件 开始
PASSED [ 33%]****************** test_aa ***********************
前置条件
PASSED [ 66%]****************** test_bb ***********************
PASSED [100%]****************** test_cc ***********************
类级别的后置条件 结束
后置条件

  5、 插件:

      unittest: 无

     pytest:  https://docs.pytest.org/en/latest/reference/plugin_list.html

@pytest.mark的用法:

  pytest.mark主要是用来标记用例,通过标记用例来实现不同的策略

  @pytest.mark.xxxxx #标记用例,执行时 可通过 pytest -m xxxx 只执行标记有改标记名的用例

  @pytest.mark.run(order=2) # 标记用例执行顺序,(需要安装 pip install pytes-ordering)

  @pytest.mark.skip # 用来标记跳过该用例

  @pytest.fixture(scope="session")【前置条件和后置条件】@pytest.mark.usefixture # usefixture无法获取 fixture的返回值

@pytest.fixture(scope="session")
def set_up():
    driver = webdriver.Chrome()
    driver.get("https://www.baidu.com")
    driver.maximize_window()
    yield driver

# usefixtures 可是调用set_up方法,但是无法使用 webdriver的返回值
@pytest.mark.usefixtures(set_up)
def test_one():
    pass

  @pytest.mark.parametrize("case_data",  case_data) # 参数化 

  @pytest.mark.flaky(rerun=5, reruns_delay)

用例筛选, 打标记。

  1) 给用例打标记

  2)在用例,类上面:@pytest.mark.标记名

    2.1 创建一个 pytest.ini文件;如图:markers 为固定格式, = 后面自定义字段

    运行: pytest -m demo -s -v

    -m "标记名"

    -s  # 输出用例信息

    -v  # 打印 print和loggin

# 坑
  在执行命令时,可能会出现导包失败的情况,需要加上如下代码

allure测试报告:

下载地址:https://repo.maven.apache.org/maven2/io/qameta/allure/allure-commandline/2.17.3/

官方文档:https://docs.qameta.io/allure#_installing_a_commandline

  可在项目包下面建个main.py

import pytest
pytest.main(["-m", "file", "-s", "-v", "--html=Outputs/reports/report.html",
             "--alluredir=Outputs/allure_reports"])
# 执行完成后, 在控制台输入如下命令查看报告

allure serve Outputs/allure_reports

posted @ 2022-03-18 13:59  JiZhaoG  阅读(87)  评论(0)    收藏  举报