How to get TestID of a test case, given the full test name

154 views
Skip to first unread message

Justin Toth

unread,
May 23, 2012, 3:38:49 PM5/23/12
to nunit-...@googlegroups.com
I am trying to create my own nunit test interface. First I grab a list of all test case names in the assembly, like so:

public List<TestName> GetTests(string assemblyPath, int runnerId)
        {
            var tests = new List<NUnit.Core.TestName>();
            //load assembly.
            var assembly = Assembly.LoadFile(assemblyPath);
            //get testfixture classes in assembly.
            var testTypes = from t in assembly.GetTypes()
                            let attributes = t.GetCustomAttributes(typeof(NUnit.Framework.TestFixtureAttribute), true)
                            where attributes != null && attributes.Length > 0
                            orderby t.Name
                            select t;
            foreach (var type in testTypes)
            {
                //get test methods in class.
                var testMethods = from m in type.GetMethods()
                                  let attributes = m.GetCustomAttributes(typeof(NUnit.Framework.TestAttribute), true)
                                  where attributes != null && attributes.Length > 0
                                  orderby m.Name
                                  select m;
                foreach (var method in testMethods)
                {
                    tests.Add(new NUnit.Core.TestName
                    {
                        RunnerID = runnerId,
                        FullName = type.FullName + "." + method.Name,
                        Name = method.Name
                    });
                }
            }
            return tests;
        }

Next I display the tests, and want to be able to click on one to run it using the RemoteTestRunner: 

//start new test.
            var testName = new TestName
            {
                FullName = fullName,
                //TestID = new TestID(???),
                Name = name,
                RunnerID = RunnerId
            };
            var filter = new NameFilter(testName);
            Runner.BeginRun(Listener, filter);

However, when passing a NameFilter into the runner on run, it requires the TestName in the NameFilter to have the TestID set. If it isn't set then the runner finds 0 test cases to run. I don't have the TestID obviously, all I have is the full test name. How do I retrieve the TestID, given the full test name, so that I can successfully run the individual test case?

Charlie Poole

unread,
May 24, 2012, 7:28:24 AM5/24/12
to nunit-...@googlegroups.com
Hi Justin,

TestNames are created by NUnit to identify tests so that you can use
them in selecting the test to run. However, you are bypassing the
NUnit load process and creating the TestNames yourself. If you could
predict that id that NUnit will subsequently assign to ta test, you
could probably get your TestNames recognized. However, it makes much
more sense to let NUnit assign the names and then use them yourself.

Use the runner you want to run the tests - RemoteTestRunner in your
case and load the tests. This will give you access to the tree of
tests. Walk the tree and create a dictionary that allows you to look
up the test based on the full name. Use that dictionary to find the
test object, from which you can grab the testname for your runs.

Charlie
> --
> You received this message because you are subscribed to the Google Groups
> "NUnit-Discuss" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/nunit-discuss/-/1czaCwkBnk4J.
> To post to this group, send email to nunit-...@googlegroups.com.
> To unsubscribe from this group, send email to
> nunit-discus...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/nunit-discuss?hl=en.
Reply all
Reply to author
Forward
0 new messages