0

I can create a single php file that uses PHPunit for assertions inside one function, inside a class. Is it possible to call functions, with each function containing an assertion?

(At the moment I'm only using Xampp and notepad++ on Windows)

fileA.php:
=============
require_once ‘fileA.php’;
testA():
testB();
testC();


fileB.php:
==========

class availabletests extends \PHPUnit_Framework_TestCase 
{

   function testA()
   {  $this->assertEquals(2,1+1); }

   function testB()
   {  $this->assertEquals(20,1+1); } 

   function testC()
   {  $this->assertEquals(8,1+1); } 

} 

Many thanks!

1
  • Generally you would run unit tests using the phpunit script: $phpunit fileB.php, and then it will give you verbose information about which tests failed and which assertions failed. To that extent, it's much better to have multiple test methods in a single file/class, as it's better for isolating each test case and allows you to look at each success/failure independently. Commented Jul 23, 2016 at 14:39

1 Answer 1

0

Not only is it possible to have multiple test methods in a single unit test, it's actually how PHPUnit is designed to be used and is preferable to putting multiple assertions into a single test. When your unit test runs via the command line test runner, this will be the output:

$ phpunit availabletests.php 
  PHPUnit 3.7.27 by Sebastian Bergmann.

  .FF

  Time: 41 ms, Memory: 3.00Mb

  There were 2 failures:

  1) availabletests::testB
  Failed asserting that 2 matches expected 20.

  /home/gwallace/availabletests.php:10

  2) availabletests::testC
  Failed asserting that 2 matches expected 8.

  /home/gwallace/availabletests.php:13

  FAILURES!
  Tests: 3, Assertions: 3, Failures: 2.

This isolates each test case and gives you verbose information on which test cases failed, and which assertions they failed on.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.