Questions tagged [phpunit]

PHPUnit is a programmer-oriented testing framework for PHP. It is an instance of the xUnit architecture for unit testing frameworks.

Latest stable version: 8.2 (April 2019)

Installation via PHAR

wget https://phar.phpunit.de/phpunit-8.phar
php phunit.phar

Installation via Composer

composer require --dev phpunit/phpunit ^8
9185 questions
383
votes
13 answers

How to run single test method with phpunit?

I am struggling to run a single test method named testSaveAndDrop in the file escalation/EscalationGroupTest.php with phpunit. I tried the following combinations: phpunit EscalationGroupTest escalation/EscalationGroupTest.php…
Alex
  • 34,021
  • 64
  • 178
  • 371
376
votes
15 answers

PHPUnit assert that an exception was thrown?

Does anyone know whether there is an assert or something like that which can test whether an exception was thrown in the code being tested?
Felipe
  • 10,031
  • 7
  • 45
  • 97
309
votes
8 answers

Best practices to test protected methods with PHPUnit

I found the discussion on Do you test private method informative. I have decided, that in some classes, I want to have protected methods, but test them. Some of these methods are static and short. Because most of the public methods make use of them,…
GrGr
  • 3,678
  • 5
  • 19
  • 20
173
votes
19 answers

How to output in CLI during execution of PHP Unit tests?

When running a PHPUnit test, I would like to be able to dump output so I can debug one or two things. I have tried the following (similar to the PHPUnit Manual example); class theTest extends PHPUnit_Framework_TestCase { /** *…
Jess Telford
  • 11,402
  • 8
  • 39
  • 50
148
votes
6 answers

Testing Abstract Classes

How do I test the concrete methods of an abstract class with PHPUnit? I'd expect that I'd have to create some sort of object as part of the test. Though, I've no idea the best practice for this or if PHPUnit allows for this.
Mez
  • 22,526
  • 14
  • 67
  • 91
147
votes
15 answers

PHPUnit: assert two arrays are equal, but order of elements not important

What is a good way to assert that two arrays of objects are equal, when the order of the elements in the array is unimportant, or even subject to change?
koen
  • 12,287
  • 9
  • 43
  • 49
146
votes
11 answers

How can I get PHPUnit MockObjects to return different values based on a parameter?

I've got a PHPUnit mock object that returns 'return value' no matter what its arguments: // From inside a test... $mock = $this->getMock('myObject', 'methodToMock'); $mock->expects($this->any)) ->method('methodToMock') …
Ben Dowling
  • 15,775
  • 8
  • 80
  • 100
138
votes
7 answers

Difference between assertEquals and assertSame in PHPUnit?

PHPUnit contains an assertEquals() method, but it also has an assertSame() one. At first glance it looks like they do the same thing. What is the difference between the two? Why are they both specified?
Kevin Burke
  • 49,451
  • 66
  • 163
  • 280
130
votes
5 answers

phpunit mock method multiple calls with different arguments

Is there any way to define different mock-expects for different input arguments? For example, I have database layer class called DB. This class has method called "Query ( string $query )", that method takes an SQL query string on input. Can I create…
Aleksei Kornushkin
  • 1,949
  • 5
  • 19
  • 18
127
votes
12 answers

SimpleTest vs PHPunit

I was wondering if anyone that has experience in both this stuff can shed some light on the significant difference between the two if any? Any specific strength of each that makes it suitable for any specific case?
paan
  • 6,868
  • 7
  • 33
  • 41
108
votes
1 answer

How to tell phpunit to stop on failure

I'm running a large suite of phpunit tests, and I'd like see which test failed as soon as it failed, rather than waiting for all of the tests to complete then having it list out all of the failures. How can I tell phpunit to do this?
johncorser
  • 7,912
  • 13
  • 51
  • 95
100
votes
8 answers

Test PHP headers with PHPUnit

I'm trying to use PHPunit to test a class that outputs some custom headers. The problem is that on my machine this:
titel
  • 3,224
  • 9
  • 42
  • 53
98
votes
25 answers

PHPUnit - 'No tests executed' when using configuration file

The Problem To improve my quality of code, I've decided to try to learn how to test my code using Unit Testing instead of my mediocre-at-best testing solutions. I decided to install PHPUnit using composer for a personal library that allows me to…
Muyiwa Olu
  • 1,301
  • 1
  • 9
  • 17
91
votes
3 answers

How to skip tests in PHPunit?

I am using phpunit in connection with jenkins, and I want to skip certain tests by setting the configuration in the XML file phpunit.xml I know that I can use on the command line: phpunit --filter…
Filype
  • 6,921
  • 7
  • 36
  • 60
89
votes
5 answers

How to run a specific phpunit xml testsuite?

how can i choose a specific testsuite to be executed? $ phpunit --configuration config.xml config.xml: library
Andreas Linden
  • 11,975
  • 7
  • 46
  • 65
1
2 3
99 100