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

Also alllow TemplateBased naming (for arguments etc.) placeholders in test description #4595

Open
rklec opened this issue Jan 10, 2024 · 3 comments

Comments

@rklec
Copy link

rklec commented Jan 10, 2024

Underlying Problem

I want to parametrize tests.

Original problem: Description for tests

NCrunch seems to display the Description instead of the TestName if that is set. So such a test looks like this:

        [TestCase(1, 1, Description = "ShouldBeTrue_WhenIsLastFoundAtSameDayAsLimit")]
        [TestCase(2, 2, Description = "ShouldBeTrue_WhenIsLastFoundAtSameDayAsLimit")]
        [TestCase(-1, -1, Description = "ShouldBeTrue_WhenIsLastFoundAtSameDayAsLimit")]
        [TestCase(1, 0)]
        public void
            ShouldBeTrue_WhenLastFoundTimeIsInThePast(int numberOfDaysLastFound, int notFoundLimitInDays)
        {

Anmerkung 2024-01-23 120711

The problem: Now ShouldBeTrue_WhenIsLastFoundAtSameDayAsLimit is just duplicated, I cannot differentiate the tests and see them in NUnit.

Try 1: Description for tests

There exist template-based placeholders like {a}:

        [TestCase(1, 1, Description = "ShouldBeTrue_WhenIsLastFoundAtSameDayAsLimit{a}")]
        [TestCase(2, 2, Description = "ShouldBeTrue_WhenIsLastFoundAtSameDayAsLimit{a}")]
        [TestCase(-1, -1, Description = "ShouldBeTrue_WhenIsLastFoundAtSameDayAsLimit{a}")]
        [TestCase(1, 0)]
        public void
            ShouldBeTrue_WhenLastFoundTimeIsInThePast(int numberOfDaysLastFound, int notFoundLimitInDays)
        {

295574947-87862955-099c-4ccd-9fdf-6855488fbc20

The problem: This does not work.

Try 2: Name of tests (workaround)

You can change it to use TestName and then you can use template-based placeholders like {a}:

295574164-fba2982c-d450-494d-a1a0-187bbedcf53f

        [TestCase(1, 1, TestName = "ShouldBeTrue_WhenIsLastFoundAtSameDayAsLimit{a}")]
        [TestCase(2, 2, TestName = "ShouldBeTrue_WhenIsLastFoundAtSameDayAsLimit{a}")]
        [TestCase(-1, -1, TestName = "ShouldBeTrue_WhenIsLastFoundAtSameDayAsLimit{a}")]
        [TestCase(1, 0)]
        public void
            ShouldBeTrue_WhenLastFoundTimeIsInThePast(int numberOfDaysLastFound, int notFoundLimitInDays)
        {     

The problem: The linked NUnit doc strongly recommends against this practice:

We strongly recommend you to NOT change the test naming.

Proposed solution

Basically make "Try 1" above work, i.e. also support placeholders like {a} in the test description.

@OsirisTerje
Copy link
Member

The reason for the recommendation to not change the testname is that the Visual Studio test explorer is driven by the testname. However, the default is {m}{a}, so not sure why that should not work for you.

The problem: Now ShouldBeTrue_WhenIsLastFoundAtSameDayAsLimit is just duplicated, I cannot differentiate the tests and see them in NUnit.

I can't see what you're pointing at. I am possibly drowning in the image there. The first image looks like it still has template parameters, but the test code doesn't. I am confused. Can you point it out?

You are correct that the template parameters are for the testname, and not for the description. I am not sure if you want this to work in VS or in NCrunch.

@rklec
Copy link
Author

rklec commented Jan 23, 2024

I am possibly drowning in the image there. [...] I am confused. Can you point it out?

Oh I am sorry seems I indeed uploaded the wrong first image I am sorry, I've corrected it in the post.

So the actual problem seems to be that NCrunch always shows the description.

Also to see how NCrunch structures tests the code structure in my example is:

[TestOf(typeof(NotFoundWithinDuration))]
internal class NotFoundWithinDurationTest
{
    // ...
    public class ShouldDelete : NotFoundWithinDurationTest
    {
      // ...

      // ....
      public void
            ShouldBeTrue_WhenLastFoundTimeIsInThePast(int numberOfDaysLastFound, int notFoundLimitInDays)
        {
           // ....
        }
   }

So that is why there is a plus in there etc, it uses the class names for it. That said, this does not matter, actually. NCrunch just always replaces the test name with the NUnit description if it has one. That is the root cause, leading to my tries here...

NCrunch v4.11.0.2 (in my case) with Visual Studio 2022 (17.8.4)

@OsirisTerje
Copy link
Member

OsirisTerje commented Jan 23, 2024

Have you raised an issue with NCrunch about this? It seems to me that the best place to fix this is in NCrunch, so that it doesnt use the description but the testname. In this case you should ignore the recommendation, given that you get a view in TestExplorer or NCrunch that you're happy with.

You could also look into moving the test data into a testcasesource, and then build up the description yourself with the correct parameters.

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

No branches or pull requests

2 participants