Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integrate PHPT test cases with PHPUnit

Tags:

php

phpunit

phpt

How can I get PHPUnit to run my PHPT test cases and integrate the pass/fail status into the overall metrics? I am already aware of how to run these tests using run-phpt from the command line, but I want to run them from within PHPUnit with my other tests.

I'm aware that the PHPUnit_Extensions_PhptTestCase exists but I couldn't find any samples on how to use it.

like image 919
edorian Avatar asked Feb 07 '11 14:02

edorian


People also ask

Which method is used to create a mock with PHPUnit?

PHPUnit provides methods that are used to automatically create objects that will replace the original object in our test. createMock($type) and getMockBuilder($type) methods are used to create mock object. The createMock method immediately returns a mock object of the specified type.

What is PHPUnit used for?

PHPUnit is a programmer-oriented testing framework for PHP. It is an instance of the xUnit architecture for unit testing frameworks. PHPUnit 9 is the current stable version. PHPUnit 10 is currently in development.

What is PHPUnit testing?

PHPUnit is a unit testing framework for the PHP programming language. It is an instance of the xUnit design for unit testing systems that began with SUnit and became popular with JUnit. Even a small software development project usually takes hours of hard work.


2 Answers

While the main point of this blog post about testing file uploads is to describe how to write PHPT tests, it shows how to integrate them into PHPUnit at the end of the post. In short, you pass the absolute path to the .phpt file to the parent constructor.

<?php
require_once 'PHPUnit/Extensions/PhptTestCase.php';

class UploadExampleTest extends PHPUnit_Extensions_PhptTestCase {
    public function __construct() {
        parent::__construct(__DIR__ . '/upload-example.phpt');
    }
}

Alternatively, you can use the PHPUnit_Extensions_PhptTestSuite class, that takes a directory as its first constructor argument and then searches for all *.phpt files within this directory.

like image 196
David Harkness Avatar answered Oct 01 '22 07:10

David Harkness


In your phpunit XML configuration, you can add a testsuite named PHPT (or any other name) and point to the directory where you placed the .phpt files with the suffix set to ".phpt". Phpunit will then execute all phpt-test-files from that directory:

<testsuites>
    <testsuite name="Main Test Suite">
        <directory>test</directory>
    </testsuite>
    <testsuite name="PHPT Test Suite">
        <directory suffix=".phpt">test/phpt</directory>
    </testsuite>
</testsuites>

In this example, the directory test/phpt contains the .phpt files.

Information about the format how .phpt tests are structured and written is available in depth at:

  • Creating new test files (PHP: Quality Assurance)
like image 42
hakre Avatar answered Oct 01 '22 06:10

hakre