Testing

Testing

We use PHPUnit to do testing.

Where to find the tests

Tests can be found in fintech/tests/Unit

Skipping tests

Add the following comment atthe top of your method inside a test to classify it as current

    /**
     * @group current
     */
     public function test_only_this_function_will_be_called()

Then run phpunit –group=current to execute just identified tests.

Shortcuts

See Bash Aliases for shortcuts available when testing

HTML Output

http://dev.getconnected.co.za/fintech/tests/Reports/index.html

Conventions

  • Test File names should have a suffix Test ** For example, if First.php needs to be tested,the name of the test file name will be FirstTest.php ** Similarly, if the class name is MyFirstClass than the test class name will be MyFirstClassTest
  • Add test as the prefix for method names. For example, if the method name is getUser(), then in test class, it will become test_get_user(). ** You can also use @test annotation in document block to declare it as a testing method
  • All testing methods are public
  • MyFirstClassTest class should be inherited from PHPUnit\Framework\TestCase.

Faker

Francois Zaninotto has the excellent PHP Faker library useful for testing. Here are some of our favourites:

<?php
$this->faker('en_ZA')->idNumber;
$this->faker('en_ZA')->mobileNumber;
$this->faker('ro_RO')->firstNameMale;
$this->faker('ro_RO')->firstNameFemale;
$this->faker('en_US')->jobTitle;
$this->faker->randomElement(['african', 'white', 'coloured', 'indian', 'foreign_national']);
$this->faker->firstName;
$this->faker->lastName;
$this->faker->freeEmailDomain;

References

https://dev.to/samsha1/how-you-test-your-code-3e4o