Overview
In this tutorial, we will create some tests in PHP using PHPUnit.
Requirements
- Install the PHP DOM extension (some distros don't include it by default)
- Install PHPUnit
Description
Calculator.php
<?php class Calculator { public function add($a, $b) { return $a + $b; } }
CalculatorTest.php
<?php require 'Calculator.php'; class CalculatorTests extends PHPUnit_Framework_TestCase { private $calculator; protected function setUp() { $this->calculator = new Calculator(); } protected function tearDown() { $this->calculator = NULL; } public function testAdd() { $result = $this->calculator->add(1, 2); $this->assertEquals(3, $result); } }
After running the tests and generating the JUnit XML report (e.g., php-results.xml), it can be imported to Xray (either by the REST API or through the Import Execution Results action within the Test Execution).
phpunit CalculatorTest.php --log-junit php-results.xml
JUnit's Test Case is mapped to a Generic Test in Jira, and the Generic Test Definition field contains the name of the test class concatenated with the method that implements the test case.
The Execution Details of the Generic Test contains information about the Test Suite, which in this case corresponds to the name of the test class.