yii2框架基于codeception对单元测试代码覆盖率测试并输出报告

298 阅读1分钟

可通过官网地址codeception.com/docs/Codeco…进行学习,官网说得很清楚,本文只是学习记录,友情提示:结合GPT高效学习。

准备工作

1、单元测试环境准备

可参考 yii2框架基于codeception进行Unit单元测试

2、下载Xdebug扩展

可参考 MAC M1芯片ARM 电脑安装PHP 7.4.33 xdebug

codeception配置文件

在项目的根目录下,有一个名为 codeception.yml 的文件,并添加以下内容:

actor: Tester
paths:
    tests: tests
    log: tests/_output
    data: tests/_data
    helpers: tests/_support
bootstrap: _bootstrap.php
settings:
    memory_limit: 1024M
    colors: true
    logging:
        enabled: true
        level: 'debug'
        path: 'tests/_output/debug.log'
modules:
    config:
        Yii2:
            configFile: 'config/test.php'
            cleanup: false

# To enable code coverage:
coverage:
   #c3_url: http://localhost:8080/index-test.php/
   enabled: true
   #remote: true
   #remote_config: '../codeception.yml'
   include:
        - controllers/*
        - biz/*
        - models/*
        - common/*
   exclude:
        - runtime/*
        - /views/*
        - tests/_output/*
        - tests/_support/*
        - tests/_data/*
        - tests/_envs/*

在上述配置中,我们启用了代码覆盖率测试,并指定了需要覆盖的代码文件和需要排除的文件。

启动测试

在终端中,进入项目的根目录,并运行以下命令:

 php vendor/bin/codecept run unit  --coverage --coverage-html

会生成一个测试报告内容文件,目录为tests/_output/coverage,打开

coverage.png

其他系列文章