I'm just test playing with Php unit.
Here is my DependencyFailureTest class:
require_once '../vendor/autoload.php';
use PHPUnit\Framework\TestCase;
class DependencyFailureTest extends \PHPUnit\Framework\TestCase
{
public function testOne()
{
$this->assertTrue(false);
}
/**
* #depends testOne
*/
public function testTwo()
{
}
}
But on running the command phpunit --verbose DependencyFailureTest it throws
Argument #3 (No Value) of PHPUnit_TextUI_ResultPrinter::__construct() must be a value from "never", "auto" or "always".
Can anybody give an explanation for this issue?
It must be a configuration issue. I copied your code and ran it on the command line with verbose and it worked fine with version 5.4.6.
I would reinstall phpunit and ensure you have the latest version.
Also, their sample test case from their Getting Started page is:
<?php
use PHPUnit\Framework\TestCase;
class MoneyTest extends TestCase
{
// ...
public function testCanBeNegated()
{
// Arrange
$a = new Money(1);
// Act
$b = $a->negate();
// Assert
$this->assertEquals(-1, $b->getAmount());
}
// ...
}
https://phpunit.de/getting-started.html
Notice the difference in your extension usage, although I don't think it is an issue, if you use their declaration as stated, it helps to isolate the problem.
I ran into this. Was passing --colors=true, but that is incorrect.
Related
Maybe it's just me but #depends doesn't seem to be working as I'd expect it to. My code:
<?php
use PHPUnit\Framework\TestCase;
class MyTest extends TestCase
{
/*
* #depends testFunc1
*/
public function testFunc2()
{
exit('TEST FUNC 2 called');
}
public function testFunc1()
{
exit('TEST FUNC 1 called');
}
}
When I do phpunit MyTest.php I'd expect to see TEST FUNC 1 called but instead I see TEST FUNC 2 called. As is it seems to just be running the tests in the order they appear in the script, regardless of the #depends attribute, which really begs the question: what does #depends actually do?
I'm running PHPUnit 5.7.20.
You need to use /** instead of /* to start a docblock.
I'm just test playing with Php unit.
Here is my DependencyFailureTest class:
require_once '../vendor/autoload.php';
use PHPUnit\Framework\TestCase;
class DependencyFailureTest extends \PHPUnit\Framework\TestCase
{
public function testOne()
{
$this->assertTrue(false);
}
/**
* #depends testOne
*/
public function testTwo()
{
}
}
But on running the command phpunit --verbose DependencyFailureTest it throws
Argument #3 (No Value) of PHPUnit_TextUI_ResultPrinter::__construct() must be a value from "never", "auto" or "always".
Can anybody give an explanation for this issue?
It must be a configuration issue. I copied your code and ran it on the command line with verbose and it worked fine with version 5.4.6.
I would reinstall phpunit and ensure you have the latest version.
Also, their sample test case from their Getting Started page is:
<?php
use PHPUnit\Framework\TestCase;
class MoneyTest extends TestCase
{
// ...
public function testCanBeNegated()
{
// Arrange
$a = new Money(1);
// Act
$b = $a->negate();
// Assert
$this->assertEquals(-1, $b->getAmount());
}
// ...
}
https://phpunit.de/getting-started.html
Notice the difference in your extension usage, although I don't think it is an issue, if you use their declaration as stated, it helps to isolate the problem.
I ran into this. Was passing --colors=true, but that is incorrect.
Can you help me? When create unit test class and my test is run okay, but I try to run test with code coverage the test always show 0%.
<?php
include 'BowlingGame.php';
class Test extends PHPUnit_Framework_TestCase {
/**
* #test
*/
public function firstTest(){
$a = new BowlingGame();
$this->assertEquals(16,$a->row(16));
}
}
Code coverage requires XDebug extension which you most likely do not have installed.
https://www.jetbrains.com/phpstorm/help/monitoring-code-coverage-for-php-applications.html
I am trying to learn how to test with phpunit and laravel. When start the test using phpunit command, I am getting a warning :
There was 1 failure:
1) Warning
No tests found in class "PostsTest".
FAILURES!
Tests: 2, Assertions: 1, Failures:
My test classname and filename matches. I have read other problems about unmatching names. my filename is PostsTest.php and my test file :
class PostsTest extends ApiTester {
public function it_fetches_posts()
{
$this->times(5)->makePost();
$this->getJson('api/v1/posts');
$this->assertResponseOk();
}
private function makePost($postFields=[])
{
$post = array_merge([
'title' => $this->fake->sentence,
'content' => $this->fake->paragragraph
], $postFields);
while($this->times --)Post::create($post);
}
}
if necessary my ApiTester :
use Faker\Factory as Faker;
class ApiTester extends TestCase {
protected $fake;
protected $times = 1;
function __construct($faker)
{
$this->fake = Faker::create();
}
}
I dont have any clue where the error is. Laravel or my local phpunit settings or anything else. Any helps is appreciated.
Thanks.
Annotations are the answer.
/** #test */
public function it_tests_something()
{
...
}
Adding that #test tells phpunit to treat the function as a test, regardless of the name.
The only methods that PHPUnit will recognize as tests are those with names starting with test.
So you should rename the it_fetches_posts() method to test_it_fetches_posts or testItFetchesPosts. The camel case naming is optional but useful if you use the --testdox option later.
Also, as stated in other answer you can also add the #test annotation to any method and it will be considered a test by PHPUnit.
Either begin its name with word 'test' like test_something_should_work or update the test docs with this annotation /** #test */
Additionally, consider a case where you are testing a class A that requires a class B (that you will mock). When $a->someMethod($mocked_B_class) is called, make sure you don't have any warnings in it such as trying to access a property of an array like you would access the property of a class ($array = ['one','two']; $array->one).
In this case it wont give you any information about the test or the error
I have the following Selenium test in PHP:
<?php
require_once('PHPUnit/Extensions/SeleniumTestCase.php');
class Example extends PHPUnit_Extensions_SeleniumTestCase
{
protected function setUp()
{
$this->setBrowser("*chrome");
$this->setBrowserUrl("http://change-this-to-the-site-you-are-testing/");
}
public function testMyTestCase()
{
$this->open("/frontend_dev.php");
try {
$this->assertTrue($this->isTextPresent("Local Coupons"));
} catch (PHPUnit_Framework_AssertionFailedError $e) {
array_push($this->verificationErrors, $e->toString());
}
}
}
When I try to run it (by running "php filename.php") I get the error:
PHP Fatal error: Class 'PHPUnit_Framework_TestCase' not found in /usr/share/php/PHPUnit/Extensions/SeleniumTestCase.php on line 60
Which makes sense because the class isn't defined anywhere, but why not? I installed PHPUnit. It seems a little weird that a class called PHPUnit_Framework_TestCase wouldn't be included with PHPUnit. Any ideas?
You should read the docs for PHPUnit for the command line test runner. You can also just run 'phpunit --help' from the command line. I believe you'll find that you need to instead run something like
phpunit path/to/filename.php