Yii2的Codeception测试-3覆盖率等

这是一个系列文章,分3部分:
1、准备工作--Yii2高级模板的安装,以及编写一个类(方法)和一个model的RETful
2、测试工作--单元测试和API(REST规范的)测试
3、力争上游--覆盖率、基础概念、引用文献等

这是第三部分

选择 Codeception的理由:

1、Yii2官方推荐和支持。
2、codeception的强大。

Codeception is a modern full-stack testing framework for PHP. Inspired by BDD, it provides an absolutely new way of writing acceptance, functional and even unit tests. Powered by PHPUnit.

覆盖率

需要安装xdebug,使用php -i | grep xdebug查看是否成功安装xdebug。
如果没有输出,需要执行sudo apt-get update;sudo apt-get install php-xdebug安装。
需要在子应用的codeception.yml,增加类似下面内容:

coverage:
   enabled: true
   whitelist:
        include:
            - models/*
            - helpers/*

执行./vendor/bin/codecept run --coverage-html
输出到 console/runtime/logs(或者tests/_output/coverage),浏览器打开里面的index.html即可
NOTE:vendor/bin/codecept run --coverage-html是整个项目都进行测试,因为执行的测试较多,速度会慢不少。
常常在run后面加上-c frontend|backend|blog|common
项目根目录下的codeception.yml中配置的paths的log节点的值(或者子应用下的codeception.yml中配置的paths的output节点的值)。

一些重要概念

Actors 演员 身份

We have a UnitTester, who executes functions and tests the code.
We also have a FunctionalTester, a qualified tester, who tests the application as a whole, with knowledge of its internals.
Lastly we have an AcceptanceTester, a user who works with our application through an interface that we provide.
其实还有一个ApiTester,用于接口测试:REST,SOAP等。

常用的测试命令:

codecept build
codecept run
codecept run -c frontend|backend|common
codecept run unit|funtional|api|acceptance
codecept run tests/acceptance/OneFileCept.php
codecept run tests/acceptance/OneFileCept.php:oneMethod
codecept run tests/acceptance/directory
codecept run tests/acceptance/directory:^someMethod
codecept run --steps --xml --htmlcodecept run  --coverage-html

除了run,generate也很常用,使用codecept -h 查看帮助文档学习。

参考:

  • Yii basic TESTING
  • Codeception for Yii framework
  • Codeception Unit Tests
  • PHPUnit docs starting from chapter 2
  • codeception Docs 从01-13

上一篇:测试工作

你可能感兴趣的:(Yii2的Codeception测试-3覆盖率等)