Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question about execution order #862

Open
nenes82 opened this issue Sep 14, 2021 · 4 comments
Open

Question about execution order #862

nenes82 opened this issue Sep 14, 2021 · 4 comments

Comments

@nenes82
Copy link

nenes82 commented Sep 14, 2021

Hello,
I have a test class with 5 functions to test.
I have a function beforeTestMethod.
My question is:
Why is beforeTestMethod executed 5 times before the test functions are executed?
I thought that beforeTestMethod was executed just before the tested function.
In the doc

The methods beforeTestMethod() and afterTestMethod() allows respectively to initialize and clean up the execution environment of the individual tests for all test method of the class. In contrast of setUp() and tearDown(), they are executed in the same subprocess

In my mind:

beforeTestMethod()
testOne()
beforeTestMethod()
testTwo()
....
beforeTestMethod()
testFive()

actual

beforeTestMethod()
beforeTestMethod()
beforeTestMethod()
beforeTestMethod()
beforeTestMethod()
testOne()
testTwo()
testThree()
testFour()
testFive()

Is it normal ?
REgards

@Grummfy
Copy link
Member

Grummfy commented Sep 14, 2021

Hello,
can you give me an example. Because it's exactly what you suppose

beforeTestMethod()
testOne()
beforeTestMethod()
testTwo()
....
beforeTestMethod()
testFive()

how did you join this conclusion?

@nenes82
Copy link
Author

nenes82 commented Sep 15, 2021

Hello,
My class:

class PluginxxxAsset extends \DbTestCase{

   public function beforeTestMethod($method) {
      global $DB;

      parent::beforeTestMethod($method);
      $config = new \PluginxxxConfig;

      switch($method) {
         case 'testDataOK':
            $config->add(['id' => 1, 
                           'endpoint' => '127.0.0.1:8020',
                           'user' => 'xxx', 
                           'password' =>'xxx',
                           'header' => 'blabla',
                           'get_from_serial_method' => 'blabla']);
            break;
         case 'testDataNOK':
            $config->add(['id' => 1, 
                           'endpoint' => '127.0.0.1:8020',
                           'user' => 'xxx', 
                           'password' => xxx),
                           'header' => 'blabla',
                           'get_from_serial_method' => 'blabla']);
            break;
         default:
            $config->add(['id' => 1, 
                           'endpoint' => 'xxx',
                           'user' => 'user', 
                           'password' => xxx,
                           'header' => 'blabla',
                           'get_from_serial_method' => 'blabbla']);
            
            break;
      }
   }

   public function afterTestMethod($method) {
      global $DB;
      parent::afterTestMethod($method);
   }

   public function testGetAssetIdBySerialOK() {
      global $DB;
      $DB->beginTransaction();
      $this
         ->given($obj = new \PluginxxxAsset())
         ->if($datas = $obj->getAssetIdBySerial('xxx'))
         ->then($this->integer($datas)->isEqualTo(xxx));
      $DB->rollBack();
   }
   public function testGetAssetIdBySerialNOK() {
      global $DB;
      $DB->beginTransaction();
      $this
         ->given($obj = new \PluginxxxAsset())
         ->if($datas = $obj->getAssetIdBySerial('xxx'))
         ->then($this->boolean($datas)->isFalse());
      $DB->rollBack();
   }
   public function testGetAssetIdBySerialEmptySerial() {
      global $DB;
      $DB->beginTransaction();
      $this
         ->given($obj = new \PluginxxxAsset())
         ->if($datas = $obj->getAssetIdBySerial(''))
         ->then($this->boolean($datas)->isFalse());
      $DB->rollBack();
   }

   public function testDataOK() {
      global $DB;
      echo "function";
      $this
         ->given($obj = new \PluginxxxAsset())
         ->if($datas = $obj->data('get_from_serial_method', false, [ 'serial_number' => 'xxx' ]))
         ->then($this->array($datas)
                        ->hasKey(0)
                     ->array($datas[0])
                        ->hasKeys(['Id', 'SerialNumber'])
                        );
   }

   public function testDataNOK() {
      global $DB;
      echo "function";
      $this
         ->given($obj = new \PluginxxxAsset)
         ->if($datas = $obj->data('get_from_serial_method', false, [ 'serial_number' => 'xxx' ]))
         ->then($this->array($datas)->size->isEqualTo(0));
   }
}

I put a breakpoint in beforeTestMethod and in each functions, It stopped 5 times in beforeTestMethod (go to each case) then it stop in each functions.
I don't know if I clear.
Regards

@Grummfy
Copy link
Member

Grummfy commented Sep 16, 2021

Thanks,
I have tested with a normal situation and an array to accumulate the passage in each method, and it was ordered properly. But I didn't test yet, with a debuging operation (xdebug I assume). So I need time to do it, and unfortunately my agenda is quiete full ;(

@nenes82
Copy link
Author

nenes82 commented Sep 16, 2021

Ok,
No problem ;)
Yes I use xdebug.
I will make the same test as you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants