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

Access current test name from skip() and/or setUp()? #20

Open
claytonrcarter opened this issue Jan 22, 2016 · 2 comments
Open

Access current test name from skip() and/or setUp()? #20

claytonrcarter opened this issue Jan 22, 2016 · 2 comments
Labels

Comments

@claytonrcarter
Copy link

I know that we can use getTests() to get a list of all tests in a UnitTestCase, but I haven't been able to figure out if it's possible to access the test/method that's currently being run, setup, or checked for skipping.

@jakoch
Copy link
Member

jakoch commented Jan 23, 2016

Hey Clayton,

that are some interesting questions.

How to get the test/method that's currently being run?

You can get the test/method that's currently being run with __FUNCTION__.
If you need the name of the test class and the name of function, its
either get_class($this). '->' . __FUNCTION__ or simply echo __METHOD__.

Check also $this->getLabel(), which retruns get_class($this),
if the test name wasn't set to the test class in its constructor.
(But this is the class name of a test case - not the individual test method name.)

How to access the test/method during setUp()?

This isn't possible from a UnitTestCase.

The name of the next test method is basically $method inside SimpleInvoker,
but there is no easy way to access it from a UnitTestCase. One could possibly
come up with some Reflection based hacks to get access to $method in SimpleInvoker.

The exec flow is skip() before setUp() before the test $method, see https://github.com/simpletest/simpletest/blob/master/invoker.php#L57

How to access access the test/method that's checked for skipping?

This isn't possible from a UnitTestCase.

There is no list of tests "checked for skipping".
You can only get the skips with a Reporter, while iterating the test methods.

For instance, the SelectiveReporter allows to run just a single test group, a single test case or even a single assertion/test within that case.
Referencing: https://github.com/simpletest/simpletest/blob/master/reporter.php#L309

Best, Jens

@claytonrcarter
Copy link
Author

Thank you. This is as I suspected. I know how to get the name of the current function, but in my case I don't want to get skip or setUp, I wanted something like testNextTestToRun because (for various complicated reasons) I needed to run just a single test depending on some other factors. I was hoping to be able to do something like

function skip() {
  $this->skipIf( $this->getCurrentTest() == "testNextTestToRun" );
}

In the end, I ended up overriding getTests() to return just the name of the test I wanted to run based on the "other factors" in play.

Thank you for the note about SelectiveReporter! I will have to have a look at it and how to use it.

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

No branches or pull requests

2 participants