Get name of test module within code

Ask general questions here.
APSoftware
Posts: 8
Joined: Tue Sep 14, 2010 10:43 am

Get name of test module within code

Post by APSoftware » Mon Apr 18, 2011 3:20 pm

Hey Guys!

As mentioned in this thread http://www.ranorex.com/forum/test-suite ... t2064.html, I want to write the status of a testmodule to teamcity in the teardown.

My question is now, is it possible to get the name of test module within the teardown?

Regards
Stefan

APSoftware
Posts: 8
Joined: Tue Sep 14, 2010 10:43 am

Re: Get name of test module within code

Post by APSoftware » Tue Apr 19, 2011 8:11 am

Problem Solved!

Code: Select all

        public string TestModuleName = GetPrivateFieldValue<string>(TestReport.CurrentTestCaseActivity, "name");

        public static T GetPrivateFieldValue<T>(Activity obj, string propName)
        {
	        if (obj == null)
	            throw new ArgumentNullException("obj");
	        
	        Type t = obj.GetType();
	        FieldInfo fi = null;
	        
	        while (fi == null && t != null)
	        {
	            fi = t.GetField(propName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
	            t = t.BaseType;
	        }
	        
	        if (fi == null)
	        	throw new ArgumentOutOfRangeException("propName",
	        		string.Format("Field {0} was not found in Type {1}", propName, obj.GetType().FullName));
	        return (T)fi.GetValue(obj);
        }

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Get name of test module within code

Post by Support Team » Tue Apr 19, 2011 8:52 am

And you don't even need the reflections you used to get the name. You can use the TestReport.CurrentTestModuleActivity.TestModuleName to get the current module name and TestReport.CurrentTestCaseActivity.TestCaseName to get the name of the current test case.

Regards,
Alex
Ranorex Team

APSoftware
Posts: 8
Joined: Tue Sep 14, 2010 10:43 am

Re: Get name of test module within code

Post by APSoftware » Tue Apr 19, 2011 9:41 am

Oh, is that easy.... :D