Difference between revisions of "Developers/Unit Testing"

From PartKeepr Wiki
Jump to: navigation, search
(Created page with "= Introduction = Most PartKeepr tests are actually functional tests. = Common Issues = == Fatal error: Call to undefined method PHPUnit_TextUI_TestRunner::printVersionStrin...")
 
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
= Introduction =
 
= Introduction =
  
Most PartKeepr tests are actually functional tests.
+
Most PartKeepr tests are actually functional tests. Because we do automated testing using [https://travis-ci.org/partkeepr/PartKeepr Travis CI], you need to specify your test database prior executing phpunit:
 +
 
 +
<pre>
 +
# Specifies the database connection to use. Other choices are mysql and pgsql as specified in [https://github.com/partkeepr/PartKeepr/blob/sf2migration/app/config/config_test.yml config_test.yml]
 +
export SYMFONY__TESTDB=sqlite
 +
vendor/bin/phpunit -c app/
 +
</pre>
 +
 
 +
You can also specify a test file by appending it to the command line:
 +
 
 +
<pre>
 +
vendor/bin/phpunit -c app/ src/PartKeepr/UnitBundle/Tests/Model/UnitTest.php
 +
</pre>
 +
 
 +
Since the functional tests use quite some disk I/O, it is recommended that you place your files on a SSD for unit testing (8 seconds vs. several minutes).
  
 
= Common Issues =
 
= Common Issues =
Line 11: Line 25:
 
==  Unable to find file "@PartKeeprFrontendBundle/Resources/public/js/" ==
 
==  Unable to find file "@PartKeeprFrontendBundle/Resources/public/js/" ==
  
Sometimes the cache messes up unit tests, as we are doing functional tests. You should [Developers/Re-generating_the_environment|regenerate the environment].
+
Sometimes the cache messes up unit tests, as we are doing functional tests. You should [[Developers/Re-generating_the_environment|regenerate the environment]].
 +
 
 +
= Writing unit tests =
 +
 
 +
Whenever you write tests, your test class should extend <code>PartKeepr\CoreBundle\Tests\WebTestCase</code>. Don't confuse it with <code>Liip\FunctionalTestBundle\Test\WebTestCase</code>, which will lead to strange problems since it loads the wrong <code>AppKernel</code>.
 +
 
 +
== Using the iri_converter ==
 +
 
 +
Since PartKeepr uses [https://www.w3.org/TR/json-ld/#iris IRIs] instead of simple numeric IDs, you should use the <code>iri_converter</code> to convert your objects to IRIs and vice versa.
 +
 
 +
Inside your tests, you can use <code>$this->getContainer()->get("api.iri_converter")</code> to retrieve the IRI converter.

Latest revision as of 14:47, 23 February 2016

Introduction

Most PartKeepr tests are actually functional tests. Because we do automated testing using Travis CI, you need to specify your test database prior executing phpunit:

# Specifies the database connection to use. Other choices are mysql and pgsql as specified in [https://github.com/partkeepr/PartKeepr/blob/sf2migration/app/config/config_test.yml config_test.yml]
export SYMFONY__TESTDB=sqlite 
vendor/bin/phpunit -c app/

You can also specify a test file by appending it to the command line:

vendor/bin/phpunit -c app/ src/PartKeepr/UnitBundle/Tests/Model/UnitTest.php

Since the functional tests use quite some disk I/O, it is recommended that you place your files on a SSD for unit testing (8 seconds vs. several minutes).

Common Issues

Fatal error: Call to undefined method PHPUnit_TextUI_TestRunner::printVersionString()

This might happen if you are using a local PHPUnit installation. You should use the phpunit version installed in

vendor/bin/phpunit

Unable to find file "@PartKeeprFrontendBundle/Resources/public/js/"

Sometimes the cache messes up unit tests, as we are doing functional tests. You should regenerate the environment.

Writing unit tests

Whenever you write tests, your test class should extend PartKeepr\CoreBundle\Tests\WebTestCase. Don't confuse it with Liip\FunctionalTestBundle\Test\WebTestCase, which will lead to strange problems since it loads the wrong AppKernel.

Using the iri_converter

Since PartKeepr uses IRIs instead of simple numeric IDs, you should use the iri_converter to convert your objects to IRIs and vice versa.

Inside your tests, you can use $this->getContainer()->get("api.iri_converter") to retrieve the IRI converter.