Is there a way to get the test that is about to be executed (the actual Function name) inside setUp() in PHPUnit?
I know that I can put the code inside the test function itself instead of setUp(), but I have created some Abstract Test Cases and since setUp() takes a long time to execute, I want to be able skip unnecessary actions or even tests inside setUp().
You can use the getName method that return the name of the test case (the function that is been called). If you pass true as arguments it will return the name of the dataset (in case of dataprovider). So simply use:
public function setUp()
{
var_dump($this->getName(false) ); // The name of the method without dataprovider
var_dump(get_called_class()); // The name of the TestCase Class
}
Hope this help
Related
In general, the macros QVERIFY, QCOMPARE, etc. shall not be used outside a test function. The reason is, that in case of a failure, they interrupt the test with a return.
If I want to do tests in a function, I should do them by hand and return false if one of them fails. Then call the function with QVERIFY. But when I do this, I miss out the detailed output of the macros like the line where the error occurred.
So I'm looking for a way to use the macros outside of a test function. One solution is to create my own macro that interrupts the test when a macro call in the underlying function fails. The main problem here is to detect when a test has failed. Looking into Qt's code, in case of a fail the variable QTest::failed is set to true. But I don't have access to this variable.
Is there a way to find out if a QtTest macro has failed?
Yeah, Qt does not really offer anything here because the test will not really get interrupted within your own function. The control flow cannot be as easily disturbed. You would need to throw an exception and make sure it's correctly caught.
What I'm doing now is just returning a const char* (works when using a string literal). If the function actually returns something, std::variant can be used, e.g.:
std::variant<MyObject, const char*> createObject() {
// do some preparations
if(preparationsFail) {
return "Error occurred";
// all worked
// ...: create some myObject
return myObject;
}
void MyTest::testFunction() {
auto result = createObject();
if(const char** error = std::get_if<const char*>(&result)) {
QVERIFY2(false, *error); // we get pointer to value, so type is **
}
// else, work with myObject by std::get<MyObject>(result);
}
Not as concise as would be desired, but it works.
It can be made more beautiful by wrapping const char* and, depending on your style, by using std::visit etc. but that's up to you and your style.
Would anybody, please, show me an example for at method in phpunit test doubles. I don't understand what is its purpose?
The purpose of the at() function is to specify the order that methods on a mock should be called. If you were to use once() or exactly(), the test would pass no matter which order the methods were called as PHPUnit is only checking that they are called during the test not when.
For example:
class FooTest extends PHPUnitTestCase {
public function testProperOrderOfMethods() {
$mockObject = $this->getMockBuilder('BarObject')
->setMethods(['baz', 'boz'])
->getMock();
$mockObject->expects($this->at(0))
->method('boz');
$mockObject->expects($this->at(1))
->method('bar');
$sut = new Foo();
$sut->methodBeingTested($mockObject);
}
This requires that our function needs to look like:
public function methodBeingTested($dependecy) {
$dependency->boz();
$dependency->bar();
}
And would fail if the function order were changed.
An example use case might be your class is using an object that connects to a service and retrieves data. You would want to have the connection opened, retrieve the data, and then close the connection. Or it may need to make further requests depending on the response. Either way all of these actions need to happen in a specific order so in your test, you would use at().
I'm trying to learn how to use Mockery with Laravel 5. I've based my efforts mostly on Way's book (Laravel Testing Decoded) and other tutorials, which say integration [with PHPUnit] only requires the tearDown() method. So I've included that. The problem is that it doesn't seem to be resetting things between tests. My test class contents look essentially like this:
public function __construct()
{
$this->mock = Mockery::mock('Class\To\Mock');
}
public function tearDown()
{
Mockery::close();
}
public function test_RedirectWithoutAuthentication()
{
// Act
$this->call('GET', '/path/1');
// Assert
$this->assertRedirectedTo('/auth/login');
}
public function test_X()
{
// Arrange
$this->mock->shouldReceive('MockedClassMethod')->once();
// Act
$this->call('GET', '/path/1');
}
The first test works and the Auth middleware kicks the user to the login page. In the interest of TDD, I've written the second test before the MockedClassMethod is actually written. So to my way of thinking, it should fail spectacularly. But it doesn't. It passes!
If I change the order of the tests, it "works" (unwritten fails, auth passes) which leads me to believe that it's not really an order problem, but something to do with one test not getting cleaned up before the next.
Any insights will save my remaining hair from being pulled out. :-)
In accordion with the PHPUnit doc:
The setUp() and tearDown() template methods are run once for each test
method (and on fresh instances) of the test case class.
The constructor is called only the first time so the tearDown is called before the second test executed of the class and the effect is that the mockery instance was closed.
In order to solve your problem you can use the setup method for init the mocked object. So replace the class constructor of the test class with the setup method as follow:
Try to use this:
protected function setUp()
{
$this->mock = Mockery::mock('Class\To\Mock');
}
instead of:
public function __construct()
{
$this->mock = Mockery::mock('Class\To\Mock');
}
Hope this help
I had tried using the setUp() method as described by Matteo, and it caused the tests not to run at all. So I abandoned that course thinking I was way off. But Matteo's suggestion turned me back to it.
A little digging into why it caused the tests to fail showed that the $app object was never getting created. Hmm... So then it dawned on me that the setUp() method was overriding some important stuff. So to fix it, all that was needed was to call the parent method first! Like so:
public function setUp()
{
parent::setUp();
$this->mock = Mockery::mock('Class\To\Mock');
}
From that point, the setUp() and tearDown() worked as expected.
I'm still not sure why all the examples I seem to find show the mock being created in the constructor. Maybe I'm still not getting something, but at least it's working... for now. :-)
I have a unit test that I am writing and have run into an annoying problem... Let's say I have the following function I am testing:
public function functionToTest(array &$data, parameter2)
{
// perform some action on the array that is being passed in by reference
}
Now, when I attempt to call this function in my unit test, I would do something like this:
public function testMyFunction()
{
$data = array('key1' => 'val1');
$mockOfClass = $this->getMockBuilder('ClassName')
->disableOriginalConstructor()
->setMethods(array('method1', 'method2')) // My function to test is NOT in this list
->getMock();
$this->mockOfClass->functionToTest($data, true);
// Perform assertions here
}
However, I receive the following error message:
Parameter 1 to ClassName::addNewFriendsToProfile() expected to be a reference, value given
This seemed very strange to me. First of all, I'm just passing an array by reference, so it it shouldn't have a problem with this. Secondly, Why parameter 1? Doesn't it mean parameter 0? Then, I tried changing the call to the following:
$this->mockOfClass->functionToTest(&$data, true);
After making this change, it works fine. Unfortunately, it also produces the following warning:
Call-time pass-by-reference has been deprecated in /PathToFile on line xxx
I do not encounter this error when running the actual code. It only throws this error in the unit test. Also, I need to use the mock as there are methods in the class I am mocking; So I can't simply create a new instance of the class and call the method that is being tested. Is there any way I can get around this?
It turns out that PHPUnit clones each of the parameters that are being passed in (Thanks Tim Lytle for pointing me to this source: Pass by reference in a callback when mocking in PHPUnit). This is what causes the error if the array is passed in without a reference at call-time in the unit test. Luckily, the solution is simple. Instead of passing the array by reference, I pass in the array by value and return the array.
before:
public function someFunction(array &$myArray)
{
$myArray[] = 'new val';
}
after:
public function someFunction(array $myArray)
{
$myArray[] = 'new val';
return $myArray;
}
PHP 5.3.10, PHPUnit 3.6.10
I am calling code in a test that fails to initialize an object so that a method may be used from it. The problem is this is legacy code, and this function is constantly used in our application. It works under PHP natively, but not when run from within PHPUnit. The calls access a PHP Extension we have for our application.
A Very simple example to demonstrate the issue is below.
Class file to wrap calls to our PHP Extension (FOO):
class FOO
{
$private $Bar;
pubilc function __construct()
{
$this->Bar = new OurPHPExtensionObject();
}
public function Option1($Variable)
{
return $this->Bar->OurPHPExtensionMethod($Variable);
}
public function _Option1($Variable)
{
return $Variable; // Dummy to test that code is loaded
}
}
$Bar = new FOO();
This creates the object and loads initial data so it is only done once. Other calls to use this include a file which then includes this class. As the application continues to run, the only initialization is done the first time, then the global keyword is used to reference this object to access it's methods.
Function.php example:
require_once('FOO.class');
function CausesError($Parameter)
{
global $Bar; // Using $Bar defined from FOO.class
$ErrorCreated = $Bar->Option1($Parameter); // Call Option from Class
}
Then, individual programs simply inclde the Function.php file with require_once() to ensure there is only 1 instance of the file loaded from anywhere.
Sample File:
require_once('Function.php');
echo CausesError('Variable Text') . "\n";
This causes the error:
Fatal error: Call to a member function Option1() on a non-object in ...
Being new to PHPUnit, and trying to minimize changes to the existing code until I can get the tests in place, I need some suggestions on how to handle this. A large amount of the existing code is calling this function by simply including the Function.php, then calling the function (CausesError). I am not sure how to get this to return the data.
My test file then has a setUp() method before each test.
class TEST_Function extends PHPUnit_Framework_TestCase
{
protected function setUp()
{
require_once('Function.php);
echo _Option1('Sample') . "\n"; // Works and ECHOs to show code loaded
echo Option1('Sample2') . "\n"; // Fails with the error
}
public function testFunctionOption1()
{
$this->assertEquals(Option1('English'), 'English'); // Error
}
}
Should I mock the function in my test? I am not sure which tests may include this since the function is used through out the code.
Hope this describes the environment and code layout. Thanks in advance for any assistance.
After reading through your question I'd assume that you're running into some other problem:
require_once('Function.php');
will ensure the file is loaded only once. So the assignment of new FOO to $Bar is done only once. As that variable therefore only exists within the scope of a single test function, it's only available with in the setUp() function.
But this might not be the case, more specifics are missing to properly say what goes on behind the scenes as you have not posted the definition of _Option1() and Option1().