pytest简单使用和生成测试报告

目录

1. 基本使用

1--安装

2--pytest书写规则

3--为pycharm设置  以 pytest的方式运行

4--setup和teardown

5--setup_class和teardown

2. pytest生成测试报告


  1. 基本使用

    1. 安装

      1. pytest文档地址
        1. pytest documentation
      2.  pip install pytest
      3. 点击pycharm左边的控制台按钮
        1. 输入pip install pytest
      4. 出现下面的情况就算成功了
    2. pytest书写规则

      1. 测试的文件名必须以test开头,或者结尾
        1. test_12312_demo.py
        2. asd_112_demo_test.pyy
      2. 测试类必须以Test开头
        1. TestLoginApi
    3. 为pycharm设置  以 pytest的方式运行

      1. 使用快捷键 ctrl + alt +s 呼出pycharm设置面板
        1. 找tools
          1. python integrated tools
            1. 找到Default test Runner 
              1. 设置pytest
                1. 最后点击ok按钮
    4. 编写一个简单的例子
      1. class TestDemo:
        
            # 定义一个测试用例
            def test_demo_001(self):
                print("这是一个测试test_demo_001")
        
            def test_demo_002(self):
                print("test_demo_002")

        1. 测试结果如下
      2. 命令行执行
        1. pytest -s .\test_demo.py
    5. setup和teardown

      1. def setup(self):
            print("前置处理")
      2. def teardown(self):
            print("后置处理")
      3. 在执行的过程中
      4. 发现这些函数并没有执行
        1. 这个的原因是因为setup 和 teardown在pytest 8.0 以后的版本已经废弃了
        2. 可以使用setup_method和 teardown_method
          1. def setup_method(self):
                print("前置处理\n")
            
            def teardown_method(self):
                print("后置处理")
        3. 运行结果
        4. 可以看到在每个方法执行前后都会执行
      5. pytest8.0以前的版本使用setup和teardown。pytest8.0以后的版本使用setup_method和teardown_method
    6. setup_class和teardown

      1. def setup_class(self):
            print("--------类级别的前置处理器---------")
        
        def teardown_class(self):
            print("--------类级别的后置处理器---------")
      2.  
      3. 可以看到只会运行一次
  2. pytest生成测试报告

    1. 安装测试报告插件
      1. pip install pytest-html
      2. 出现下面的情况就算安装完成了
    2. 使用指令生成测试报告
      1. pytest --html=.\reports.html .\test_demo.py
      2. pytest --html=生成报告的路径 执行测试用例的路径
      3. 打开测试报告
        1. 找到reports.html文件 
          1. 鼠标右键
            1. Open in
              1. Brower
                1. Chrome
                2. 这里可以选择你安装的浏览器去打开
                3. 我这里安装了Chrome,就选择了Chrome
      4. 可以看到生成的测试报告
      5. 可以看到这些结果展示还是很直观 的
使用 `pytest` `Allure` 生成测试报告,需要按照以下步骤操作: ### 安装必要的工具插件 首先,确保安装了 `pytest` `allure-pytest` 插件。可以通过以下命令进行安装: ```bash pip install pytest allure-pytest ``` 此外,还需要安装 Allure 命令行工具来生成查看报告。对于不同的操作系统,可以使用相应的包管理器或下载二进制文件进行安装。 ### 编写测试用例 编写一些简单的测试用例,并确保它们能够通过 `pytest` 运行。例如,创建一个名为 `test_example.py` 的文件,并添加以下内容: ```python def test_success(): assert True def test_failure(): assert False ``` ### 运行测试并生成结果 在运行测试时,需要指定 `--alluredir` 参数来保存 Allure 所需的原始数据。执行以下命令: ```bash pytest --alluredir=./allure-results test_example.py ``` 此命令会将测试结果输出到 `./allure-results` 目录中。 ### 生成 Allure 报告 使用 Allure 命令行工具生成 HTML 格式的报告。执行以下命令: ```bash allure generate ./allure-results -o ./allure-report --open ``` 这条命令会读取 `./allure-results` 中的数据,并将其转换为 HTML 格式,输出到 `./allure-report` 目录中。`--open` 参数会在浏览器中自动打开生成报告。 ### 自定义报告内容 为了使报告更加丰富,可以在测试用例中使用 `allure` 提供的各种装饰器来添加描述、步骤、附件等信息。例如: ```python import allure @allure.step("Step description") def step_inside_test(): return True @allure.feature("Feature name") @allure.story("Story name") def test_custom_report(): with allure.step("Another step"): result = step_inside_test() assert result ``` 这些装饰器可以帮助更好地组织测试用例,并在报告中显示更详细的上下文信息。 ### 集成到 CI/CD 流程 在持续集成/持续部署(CI/CD)流程中,可以通过脚本自动化上述过程。通常,在 Jenkins、GitLab CI 或 GitHub Actions 等平台上配置相应的步骤即可实现自动化的测试报告生成。 ### 示例:完整的测试用例与报告生成 假设有一个包含多个测试用例的文件 `test_case.py`,其内容如下: ```python import allure @allure.feature("Login Functionality") class TestLogin: @allure.story("Valid Login") def test_valid_login(self): assert True @allure.story("Invalid Login") def test_invalid_login(self): assert False ``` 运行测试并生成报告: ```bash pytest --alluredir=./allure-results test_case.py allure generate ./allure-results -o ./allure-report --open ``` 最终,生成报告将展示每个测试用例的详细信息,包括功能模块、故事名称以及具体的测试步骤[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值