Merge multiple coverages for Kahlan with istanbul merge
As you may already knew, you can generate HTML coverage report with kahlan with the following command:
$ ./bin/kahlan --src=path/to/src --spec=path/to/spec --istanbul="coverage.json" $ istanbul report
For multiple src path, with different specs location, as there are multiple coverages, we need to merge them. For example, we have the following application structure:
. └── module ├── A │ ├── spec │ │ └── ASpec.php │ └── src │ └── A.php └── B ├── spec │ └── BSpec.php └── src └── B.php
Prepare dependencies
1. Install the following tools:
2. Install kahlan/kahlan:^3.0
$ composer require kahlan/kahlan:^3.0 --dev --sort-packages
Here is the sample of composer.json
:
// composer.json { "name": "samsonasik/kahlan-demo", "type": "project", "require-dev": { "kahlan/kahlan": "^3.0" }, "autoload": { "psr-4": { "A\\": "module/A/src", "B\\": "module/B/src" } }, "autoload-dev": { "psr-4": { "ASpec\\": "module/A/spec", "BSpec\\": "module/B/spec" } }, "license": "MIT", "authors": [ { "name": "Abdul Malik Ikhsan", "email": "samsonasik@gmail.com" } ], "config": { "bin-dir": "bin" }, "minimum-stability": "dev", "prefer-stable": true }
Write specs
We can write specs under module/{FOLDER}/spec.
Define Tasks on build.xml
We need to register multiple commands in build.xml
- run bin/kahlan for module/A/src which set coverage target to coverage/coverge-A.json
- run bin/kahlan for module/B/src which set coverage target to coverage/coverge-B.json
- run instanbul-merge that merge coverage/coverage*.json to coverage.json
- run instanbul report
The build.xml can be like the following:
<?xml version="1.0" encoding="UTF-8"?> <project name="kahlan-demo app" default="build"> <property name="toolsdir" value="${basedir}/bin/"/> <property name="moduledir" value="${basedir}/module/"/> <target name="build" depends="kahlan,coverage-report" description=""/> <target name="kahlan" description="Run kahlan"> <!-- A --> <exec executable="${toolsdir}kahlan" failonerror="true" taskname="kahlan"> <arg line="--spec=${moduledir}A/spec/ --src=${moduledir}A/src --istanbul=coverage/coverage-A.json "/> </exec> <!-- A --> <!-- B --> <exec executable="${toolsdir}kahlan" failonerror="true" taskname="kahlan"> <arg line="--spec=${moduledir}B/spec/ --src=${moduledir}B/src --istanbul=coverage/coverage-B.json "/> </exec> <!-- B --> </target> <target name="coverage-report" description="Run coverage report generation"> <!-- merging coverage under coverage/ to coverage.json --> <exec executable="istanbul-merge" failonerror="true" taskname="istanbul merge"> <arg line="--out coverage.json coverage/*.json"/> </exec> <!-- generate report with use of merged coverages to coverage.json --> <exec executable="istanbul" failonerror="true" taskname="istanbul report"> <arg line="report"/> </exec> </target> </project>
Run tasks
We can run ant command and we will get the following output:
$ ant Buildfile: /Users/samsonasik/www/kahlan-demo/build.xml kahlan: [kahlan] _ _ [kahlan] /\ /\__ _| |__ | | __ _ _ __ [kahlan] / //_/ _` | '_ \| |/ _` | '_ \ [kahlan] / __ \ (_| | | | | | (_| | | | | [kahlan] \/ \/\__,_|_| |_|_|\__,_|_| | | [kahlan] [kahlan] The PHP Test Framework for Freedom, Truth and Justice. [kahlan] [kahlan] Working Directory: /Users/samsonasik/www/kahlan-demo [kahlan] [kahlan] . 1 / 1 (100%) [kahlan] [kahlan] [kahlan] [kahlan] Expectations : 1 Executed [kahlan] Specifications : 0 Pending, 0 Excluded, 0 Skipped [kahlan] [kahlan] Passed 1 of 1 PASS in 0.074 seconds (using 2Mo) [kahlan] [kahlan] Coverage Summary [kahlan] ---------------- [kahlan] [kahlan] Total: 100.00% (1/1) [kahlan] [kahlan] Coverage collected in 0.002 seconds (using an additionnal 70Ko) [kahlan] [kahlan] [kahlan] _ _ [kahlan] /\ /\__ _| |__ | | __ _ _ __ [kahlan] / //_/ _` | '_ \| |/ _` | '_ \ [kahlan] / __ \ (_| | | | | | (_| | | | | [kahlan] \/ \/\__,_|_| |_|_|\__,_|_| | | [kahlan] [kahlan] The PHP Test Framework for Freedom, Truth and Justice. [kahlan] [kahlan] Working Directory: /Users/samsonasik/www/kahlan-demo [kahlan] [kahlan] . 1 / 1 (100%) [kahlan] [kahlan] [kahlan] [kahlan] Expectations : 1 Executed [kahlan] Specifications : 0 Pending, 0 Excluded, 0 Skipped [kahlan] [kahlan] Passed 1 of 1 PASS in 0.045 seconds (using 2Mo) [kahlan] [kahlan] Coverage Summary [kahlan] ---------------- [kahlan] [kahlan] Total: 100.00% (1/1) [kahlan] [kahlan] Coverage collected in 0.001 seconds (using an additionnal 70Ko) [kahlan] [kahlan] coverage-report: [istanbul report] Done build: BUILD SUCCESSFUL Total time: 2 seconds
Now, we have successfully gotten merged coverage results with open coverage/lcov-report/index.html
:
leave a comment