1

I am using Codeception to test 3 APIs:

  1. Write a post comment
  2. Write a reply to the comment
  3. Get all comments from a post

The first API returns the ID of the comment that was just inserted into DB. I want to pass the comment Id to the second test method.

In PHPUnit I would do that by returning the commentId from the first method and annotating the second method with @depends, but in Codeception @depends does not send the returned value from the first method.

Is there any way I could send the value without placing both tests in the same method?

I am just starting to use Codeception, so I might miss some valuable information.

2 Answers 2

4

Codeception runs all your tests in the same memory space, so you can just use the GLOBAL keyword:

/**
 * @test1
 */
public function testOne(){
    global $hello;
    $hello = "Hello!";
}

/**
 * @test2
 */
public function testTwo(){
   global $hello;
   $this->assertEquals("Hello!", $hello);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Using global variables should be avoided. Rather create a private method in the test and pass the var(s) to it;
1

This works fine and is very straightforward. Note that functions that should not be contained in the test should be protected

 protected $var1;
 protected $var2;

public function test1(AcceptanceTester $I){
    $var1 = "Variable1";
    $var2 = "Variable2";
    $this->pass_variable($var1,$var2);
}

public function test2(AcceptanceTester $I){
    $I->doSomethingWithPassedVars($this->var1, $this->var2);
}

protected function pass_variable($var1 = "", $var2 = ""){
    $this->var1 = $var1;
    $this->var2 = $var2;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.