Codeception: Hide method from being executed as Test

Created on 22 Jul 2016  Â·  1Comment  Â·  Source: Codeception/Codeception

Here are my tests results:

Api Tests (3) ----------------------------------------------------------------
✔ FirewallSettingsCest: It_should_create_new_firewall_setting (0.71s)
✔ FirewallSettingsCest: It_should_update_a_single_setting_that_exists (0.15s)
✔ FirewallSettingsCest: Token (0.13s)

As you can see, the last "Test" is a method called Token. This method is inherited from a Trait

class FirewallSettingsCest {

    use MakeTokens;

    ....

}

According to the Docs, I can only hide a method from being executed as a Test if the method is not public or starts with underscore _. I don't want my Trait method called _token just because one of the 10 libraries I make usage of wants it to be like that.

Problem

If a Cest Test class makes usage of traits that have public methods, those methods will be tested against. It is unreliable to trust that those methods will never throw any kind of exception when invoked by Codeception.

Proposal

An attribute called $exclude that should be declared as public where we can set the names of methods that should not be executed as tests. That way I would have:

class FirewallSettingsCest {

    use MakeTokens;

    public $exclude = ['token'];
    ...
}

Possible Solution

On file Codeception\Test\Loader\Cest.php lines 37~40 it currently reads

            $unit = new $testClass;

            $methods = get_class_methods($testClass);
            foreach ($methods as $method) {

A possibility would be

            $unit = new $testClass;

            $methods = get_class_methods($testClass);
            if(isset($unit->exclude) && is_array($unit->exclude))
                $methods = array_diff($methods, $unit->exclude);

            foreach ($methods as $method) {

This way we can:

A) Prevent legacy code from breaking because we're making sure that the attribute exists before using it. If your Cest class don't have it, nothing will change.
B) Allow methods that we don't want to run as tests without relying on visibility modifier or name.
C) Tackle this problem without significant changes in the way Codeception is now.


That's all I wanted to say. I would just like to point out that I'm fairly new to the TDD world as well as open-source contributions. If this, somehow, looks like a stupid proposal, please let me know where I went wrong so I can learn, improve and evolve.

Most helpful comment

It is much easier to redefine visibility of methods when importing them from a trait:

 use MakeTokens { token as protected; }

http://php.net/manual/en/language.oop5.traits.php#language.oop5.traits.visibility

>All comments

It is much easier to redefine visibility of methods when importing them from a trait:

 use MakeTokens { token as protected; }

http://php.net/manual/en/language.oop5.traits.php#language.oop5.traits.visibility

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sebastianneubert picture sebastianneubert  Â·  3Comments

centerax picture centerax  Â·  4Comments

DavertMik picture DavertMik  Â·  4Comments

joshhornby picture joshhornby  Â·  3Comments

allen0817 picture allen0817  Â·  3Comments