Just assigning null to the obj is what done in tearDown().What all the entries created in db by other function while testing that will be deleted and for that we should write in tearDown() this was I thought.
protected function setUp()
{
$this->XSCCategoryModelObj = new XSCCategoryModel();
}
protected function tearDown()
{
$this->XSCCategoryModelObj =null;
}
I believe that this section from documentation answers on your question. Also, this part with explanation can help:
The setUp() and tearDown() template methods are run once for each test method (and on fresh instances) of the test case class.
In addition, the setUpBeforeClass() and tearDownAfterClass() template methods are called before the first test of the test case class is run and after the last test of the test case class is run, respectively.
Related
I know that unit tests should run isolated and should never depend on other unit tests.
However, I also write some integration tests with MSTest and sometimes they produce a result that I would like to reuse in another test.
For example:
Creating a user
Searching this user from the database
Deleting the user
Each of those points would be an integration test for me, so I would like to write methods that look like this:
User _myNewUser;
[TestMethod]
public void CreateAUserTest()
{
//User gets created here somehow....
_myNewUser = successfullyCreatedUser;
}
And this test should run after the preceeding test:
User _myNewUser;
[TestMethod]
public void SearchingUserTest()
{
var user = searchUser(_newUser.GetName());
//Assert that user is not null
}
You can see that I use the value of the first test in the second test.
With a playlist I could make sure that both tests run in the correct order.
However, in VS 2022 each test gets executed in isolation, so what I am trying to do does not work.
_newUser is always null if I run the second test, even if the first test was a success.
Is my idea bad in general?
If not: How can I use the produced data of a test in another test?
I usually extract the contents of a test like that into a separate staging function that does not contain the [TestMethod] attribute, so that I can reuse it to stage other tests.
private void Stage_CreateAUser()
{
//do work from CreateAUserTest()
}
[TestMethod]
public void CreateAUserTest()
{
Stage_CreateAUser();
}
private void Stage_SearchingUser()
{
//do work from CreateAUserTest()
}
[TestMethod]
public void SearchingUserTest()
{
Stage_CreateAUser();
Stage_SearchingUser();
}
Etc...
I have some setup specific to my test in a class. Since it is specific to my test, I have added to it to the top of my test function. The cleanup is added to the end of the test function. The problem when the test fails and the cleanup does not gets executed. Is there a PHPUnit way to specify a custom teardown specific to my test function. I looked at the PHPUnit manual which specifies teardownAfterClass and tearDown, but both does not solve my problem. The function teardownAfterClass will run only once at the end of the class. The function teardown runs after each test, but I do not want to do any cleanup if my specific test function was not executed.
What is the PHPUnit way of creating custom teardown function for my test?
Here is the code which I use to make sure that the cleanup specific to the test always happen, but it is ugly as it needs to put the actual test in a separate function and needs try/catch block. Is there is a PHPUnit specific way to handle it? Something like dataProvider specific to function will be great, which always gets executed after the test irrespective of failure or success.
class testClass {
public function test1() {
self::setupSpecificToTest1();
try {
// actual test without cleanup
$this->_test1();
} catch (Exception $e) {
self::cleanupSpecificToTest1();
throw $e;
}
self::cleanupSpecificToTest1();
}
public function test2() {
// some code which does not need any setup or cleanup
}
private function _test1() {
// some test code
}
}
Implement intelligent tearDown (it's closer to PHPUnit's approach to running tests)
You can check for the specific test name inside tearDown method to alternate its behaviour accordingly. Getting test name can be done via $this->getName() in the test class.
Try something like:
...
public function test1() { ... }
public function test2() { ... }
public function tearDown()
{
if ($this->getName() === 'test1')
{
// Do clean up specific to test1
}
parent::tearDown();
}
I have tried this, it worked for me after my test.
public function tearDown()
{
$this->webDriver->close();
}
I'm currently trying to pass data from my data provider to the setUp()-method in PHPUnit.
Background: I am using PHPUnit for running frontend-tests in different browsers. The browser should be defined inside the data provider and needs to be known by the setUp()-method.
I understand, that a data provider initially is executed before the setUp()-method (as setUpBeforeClass()) is called. Therefore setUp()-data can not be passed to a data provider. But it should work the other way round, shouldn't it?
Does PHPUnit generate its own temporarily testclasses with data from the data provider "integrated"?
Of course: a workaround could be, to read the XML-file in the setUp()-method again. But that's the last option, I'd consider...
EDIT: Provided a small snippet:
part of dataProvider():
public function dataProvider()
{
$this->xmlCnf = $data['config'];
var_dump($this->xmlCnf); // array with config is exposed
// [...]
}
And the setUp()-method:
protected function setUp()
{
var_dump($this->xmlCnf); // NULL
//[...]
}
In case this is useful to anyone:
The following code should work:
public function dataProvider()
{
return [ [ /* dataset 1 */] , ... ]
}
protected setUp() {
parent::setUp();
$arguments = $this->getProvidedData();
// $arguments should match the provided arguments for this test case
}
/**
* #dataProvider dataProvider
*/
public function testCase(...$arguments) {
}
The getProvidedData method seems to have been available since PHPUnit 5.6 (which was either shortly before or after this question was originally asked)
we can make the xmlCnf to static
private static $xmlCnf;
public function provider(){
self::$xmlCnf = 'hello';
var_dump(self::$xmlCnf); //hello
return [...];
}
public function setUp() {
var_dump(self::$xmlCnf); //hello
parent::setUp();
}
I am curious to know it is good practice to create object in test class __construct or we should always use setup/teardown approach ( or setUpBeforeClass/tearDownAfterClass approach)?
I aware of the fact set/teardown gets called for each test so will it do any good if I put my object creation code in it? e.g.
//mytestclass.php
class MyTestClass extends PHPUnit_Framework_TestCase
{
private $obj;
protected function setUp()
{
$this->obj = new FooClass();
}
public testFooObj()
{
//assertions for $this->obj
}
...
}
what could be the issues if I create object in constructor like this:
class MyTestClass extends PHPUnit_Framework_TestCase
{
private $obj;
protected function __construct()
{
$this->obj = new FooClass();
}
public testFooObj()
{
//assertions for $this->obj
}
...
}
I tried googling around as well as PHPUnit documentation couldn't get much information about, Can you please help me to understand which one is good practice?
setUp() gets called before each of your tests is ran. __construct() happens when your class is instantiated. So if you have multiple tests and they use local properties and modify them, using setUp() you can ensure that they are the same before each test is ran. The opposite of setUp() is tearDown() where you can ensure that test data gets cleaned up after each test.
As I have just found out, implementing the default class constructor instead of the setupBeforeClass() method breaks the #dataProvider annotations (probably all kinds of annotations), yielding a "Missing argument" exception for any parameterized tests.
Missing argument 1 for AppBundle\Tests\Service\InvitationVerifierTest::testDireccionInvalida()
Replacing public function __construct() for public static function setUpBeforeClass() gets rid of the exception. So there it goes, favor the setupBeforeClass() method over the regular constructor.
PHPUnit version 4.5.0
PhpUnit::How can be __construct with protected variables tested?
(not always we should add public method getVal()- soo without add method that return protected variable value)
Example:
class Example{
protected $_val=null;
function __construct($val){
$this->_val=md5 ($val);
}
}
Edit:
also exist problem to test in function that return void
Edit2:
Example why we need test __construct:
class Example{
protected $_val=null;
//user write _constract instead __construct
function _constract($val){
$this->_val=md5 ($val);
}
function getLen($value){
return strlen($value);
}
}
class ExampleTest extends PHPUnit_Framework_TestCase{
test_getLen(){
$ob=new Example();//call to __construct and not to _constract
$this->assertEquals( $ob->getLen('1234'), 4);
}
}
test run ok, but Example class "constructor" wasn't created!
Thanks
The main goal of unit testing is to test interface By default, you should test only public methods and their behaviour. If it's ok, then your class is OK for external using. But sometimes you need to test protected/private members - then you can use Reflection and setAccessible() method
Create a derived class that exposes the value that you want to test.