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

Add testcase method's name to the test's title #451

Merged
merged 1 commit into from
Jun 18, 2024

Conversation

smuuf
Copy link
Contributor

@smuuf smuuf commented Jun 12, 2024

Resolves #448.

  • new feature
  • BC break? no (hopefully)

What this does

For tests wrapped in testcase class:

class MyTests extends \Tester\Testcase {
    public function testMethod_1() { ... }
    public function testMethod_2() { ... }
}

... where Tester creates a separate Test object for each of the testcase's methods, we now add (append) the method's name to the (maybe already specified) test's title.

In the case of these two methods, if this PR is merged, the first test object will now have the title "testMethod_1" and the second one will have the title "testMethod_2".

What is looks like

... if Nette Tester self-tests with -o console-lines output mode:
obrazek

Append... title?

Yes, the test file might also contain "a primary, standalone title definition", like so:

/**
 * These are my tests
 */
class MyTests extends \Tester\Testcase {
    public function testMethod_1() { ... }
    public function testMethod_2() { ... }
}

... in that case the test objects representing both methods would have a same, pre-specified title "These are my tests".

This PR changes this and in the end the first test object would have the title "These are my tests testMethod_1" and the second one will have the title "These are my tests testMethod_2".

@smuuf smuuf force-pushed the testcasemethodnameintesttitle branch from d7efad8 to 41a9b32 Compare June 12, 2024 20:31
@dg dg force-pushed the master branch 3 times, most recently from 9f3ba77 to 7fd3b98 Compare June 18, 2024 16:51
@dg dg merged commit bdb376f into nette:master Jun 18, 2024
14 of 15 checks passed
@@ -43,7 +43,7 @@ class Test
public function __construct(string $file, ?string $title = null)
{
$this->file = $file;
$this->title = $title;
$this->title = $title !== null ? trim($title) : null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In what case is the constructor called with a title that has extra spaces?

dg pushed a commit that referenced this pull request Jun 18, 2024
dg pushed a commit that referenced this pull request Jun 18, 2024
@smuuf
Copy link
Contributor Author

smuuf commented Jun 19, 2024

Thanks for merging 🌻 Unfortunately your changes introduced a minor bug with dealing with spaces in the test's title:

7d226fa#diff-07c33aaa3cca21e4927002dc11edb6d232627948635ad1e537854703ecd21666R231-R233

	return array_map(
			fn(string $method): Test => $test
				->withTitle("$test->title $method")
				->withArguments(['method' => $method]),
			$methods,
		);

... If the initial instance of Test doesn't have any "primary" title (i.e. $test->title is null or ''), then the new title will contain leading whitespace.

obrazek

It would probably be sufficient if it was done as ->withTitle(trim("$test->title $method")) instead.

@smuuf smuuf deleted the testcasemethodnameintesttitle branch June 19, 2024 08:48
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

Successfully merging this pull request may close these issues.

Display name of concrete test method in console-lines output
2 participants