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 support for targeting generic test fixtures #6

Open
jcansdale opened this issue Jul 18, 2016 · 9 comments
Open

Add support for targeting generic test fixtures #6

jcansdale opened this issue Jul 18, 2016 · 9 comments

Comments

@jcansdale
Copy link
Contributor

Is it currently running no tests when the following it targeted:

    [TestFixture(typeof(string))]
    public class GenericTestFixtures<T>
    {
        [Test]
        public void IsTypeOfString()
        {
            Assert.That(typeof(T), Is.TypeOf<string>());
        }
    }

@jcansdale
Copy link
Contributor Author

jcansdale commented Jul 18, 2016

I'm guessing this will be something to do with the names NUnit uses for generic fixtures. The adapter will assume the above has a test path of GenericTestFixtures```1[T]. If anyone see this and knows what the correct test path should be, please chime in. :) I'll continue investigating...

@jcansdale
Copy link
Contributor Author

It seems generic test fixtures are named in a similar way to their C# source code, so the above has the path: GenericTestFixtures<T>.

The following code resolves this:

        static string toTestPath(Type type)
        {
            var testPath = type.FullName;

            if (type.IsGenericTypeDefinition)
            {
                testPath = type.FullName.Split('`')[0];
                testPath += "<";
                foreach(var arg in type.GetGenericArguments())
                {
                    if(!testPath.EndsWith("<"))
                    {
                        testPath += ",";
                    }

                    testPath += arg.Name;
                }
                testPath += ">";
                return testPath;
            }

            return testPath;
        }

@jcansdale
Copy link
Contributor Author

Unfortunately I now have an issue targeting the methods of generic types. Because the full name of the test method includes the expanded generic type (in this case GenericTestFixtures<String> not GenericTestFixtures<T>), finding a test path for the target method becomes more tricky. 😭

I guess I'll have to start using --where for test methods in generic types. Hopefully the following will work:

--where "method == 'IsTypeOfString' && class == 'GenericTestFixtures`1'"

@CharliePoole
Copy link
Member

Could you use the test ids? Perhaps by initializing some sort of dictionary?

@jcansdale
Copy link
Contributor Author

@CharliePoole,

Could you use the test ids? Perhaps by initializing some sort of dictionary?

Would that be using ITestRunner.Load and looking for IDs in the returned XML node?

I've got targeting methods in a generic type working using, class == '{0}' && method == '{1}'. It works fine except for if the target method has an [Explicit] attribute (where it doesn't recognize it as being explicitly targeted). This is a bit of an edge case, so I'm not going to worry too much. I'll continue targeting methods in non-generic classes using test == '{0}', which works fine with the [Explicit] attribute.

I haven't spotted a way to tell the runner which method or class is being explicitly targeted. Is this a special case of the test == {0} rule?

@CharliePoole
Copy link
Member

I assume you are doing this by creating a filter? If the filter directly selects an explicit test it should run if it selects a parent it should not. However, we might have a bug with combining a class level filter with a method level one.

@jcansdale
Copy link
Contributor Author

However, we might have a bug with combining a class level filter with a method level one.

It's when where is set to: class == '{0}' && method == '{1}' (and they're both exact matches).

@CharliePoole
Copy link
Member

Right... that's what I meant. If the resulting andfilter doesn't show an exact match to the method, you should create an issue for it.

@jcansdale
Copy link
Contributor Author

jcansdale commented Jul 18, 2016

Done. nunit/nunit#1684

jcansdale added a commit that referenced this issue Jul 18, 2016
…s targeted #4

Add support for executing tests in concrete sub-classes when abstract class is targeted #5
Add support for targeting generic test fixtures #6
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

No branches or pull requests

2 participants