In the following article I will use continuous integration for quality control: I want to make sure my projects are building and their unit tests are passing. I’ve configured CI in a project of mine for both the C# and the php part. Here’s how:
C#
I’m using appveyor for this. Its as easy as going to their webpage, and adding your project from github. It will automatically figure out what and how to build. You can customize the actions appveyor executes with an appveyor.yml
file in your project root. In the following example I customized appveyor to work with codecov.io to get code coverage of the unit tests:
os: Visual Studio 2017 install: - set PATH=C:\Program Files (x86)\MSBuild\14.0\Bin;%PATH% before_build: - nuget restore build: project: Famoser.SyncApi.sln verbosity: minimal test_script: - .\packages\OpenCover.4.6.519\tools\OpenCover.Console.exe -register:user -target:"C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\MSTest.exe" -targetargs:"/noresults /noisolation /testcontainer:"".\Famoser.SyncApi.UnitTests\bin\Debug\Famoser.SyncApi.UnitTests.dll" -filter:"+[Famoser.SyncApi]* -[Famoser.SyncApi]Famoser.SyncApi.Properties.*" -excludebyattribute:*.ExcludeFromCodeCoverage* -hideskipped:All -output:.\SyncApi_coverage.xml -returntargetcode - "SET PATH=C:\\Python34;C:\\Python34\\Scripts;%PATH%" - pip install codecov - codecov -f "SyncApi_coverage.xml" # preserve "packages" directory in the root of build folder but will reset it if packages.config is modified cache: - packages -> **\packages.config
php
I’m using travis-ci.org to build & test my application, and codeclimate to check my coding style. As usual with web projects, it needs a little more configuration to get up and running.
codeclimate
For this service, you register your project on the webpage of codeclimate, and create a config file named codeclimate.yml
. In it, you must specify the paths of your php files, and the engines you want to look at your code. A ready to use config file may look like this:
engines: fixme: enabled: true phpcodesniffer: enabled: true phpmd: enabled: true duplication: enabled: true config: languages: - php ratings: paths: ["Famoser.SyncApi.Webpage/src/**/*"] exclude_paths: ["Famoser.SyncApi.Webpage/src/public/assets/**/*"]
travis
First, you add a config file called .travis.yml. You need to specify the language and the versions of the language to use for testing. You can also specify scripts which run before / after various events. After the configuration is done, add the project on travis-ci.org.
For my setup with code climate & composer I need some additional config: I need to install composer dependencies and execute a script to push test results to codeclimate. For the connection with codeclimate to work be sure to include
- an environment variable on travis-ci.org (under travis-ci -> settings) with key
CODECLIMATE_REPO_TOKEN
and valueyour_test_reporter_token
(you may find this token on codeclimate -> settings -> Test Coverage). - a new composer dependency under
require-dev
:"codeclimate/php-test-reporter": "dev-master"
- the after_script part in the
.travis-yml
as specified below.
a ready to use configuration may look like this:
language: php php: - 7.0 - 7.1 install: - cd Famoser.SyncApi.Webpage - composer install --dev after_script: - ./vendor/bin/test-reporter
For the automatic execution of phpunit tests, configure phpunit. A sample configuration file looks like this:
<?xml version="1.0" encoding="UTF-8"?> <phpunit bootstrap="vendor/autoload.php" stopOnFailure="false"> <testsuites> <!-- following specifies to executes all files in the tests directory which end in Test.php, for example SimpleTest.php --> <testsuite name="Application Test Suite"> <directory suffix="Test.php">./tests</directory> </testsuite> </testsuites> <php> <!-- specify php environment values --> <!-- <env name="TESTING_MODE" value="testing"/>--> </php> <logging> <!-- this is important for the code climate connection --> <log type="coverage-clover" target="build/logs/clover.xml"/> </logging> <filter> <!-- whitelist all php source files you may use in the process of building & testing --> <whitelist> <directory suffix=".php">src</directory> </whitelist> </filter> </phpunit>