How to get current test case name

Ask general questions here.
xibinki
Posts: 30
Joined: Mon Jun 27, 2016 9:59 am

How to get current test case name

Post by xibinki » Mon Jul 29, 2019 5:12 pm

Hello Ranorex,

I'm trying to save a .txt file with durations of my tests into a specific folder for each of my test cases.
For example:
1. I want to run the test_0001
2. I want to save the duration of it into a .txt file

The code of my test_0001 is something like:

Code: Select all

getStartTime();
DoStuff();
getEndTime();
On my getEndTime method, at the end of it I have:

Code: Select all

System.IO.File.AppendAllText(@"C:\Users\x\Documents\" + TestSuite.CurrentTestContainer.Name + ".txt", Environment.NewLine + "Duration: " + s + Environment.NewLine);
The problem here, is that "TestSuite.CurrentTestContainer.Name" always gives me NULL value :(
How do I get the current test name that I'm running?

Edit 1:
I noticed that if I run the test from the Test Suite, my code works!
But if run the test directly in the module code, I get the null value for the test name.
Last edited by xibinki on Tue Jul 30, 2019 10:02 am, edited 1 time in total.

User avatar
Stub
Posts: 515
Joined: Fri Jul 15, 2016 1:35 pm

Re: How to get current test case name

Post by Stub » Tue Jul 30, 2019 8:26 am

Are you running just your code module where you call TestSuite.CurrentTestContainer? Ie the big "Run Code" button from the code editor? If so then there simply is no Test Suite at that point. You have to run your test from the Test Suite using the Run button, or Run Selected Smart Folder/Run Selected Test Case via the right-click context menu.

Nowadays I code any code module that tries to access the TestSuite for whatever reason to anticipate that it's being run directly.

xibinki
Posts: 30
Joined: Mon Jun 27, 2016 9:59 am

Re: How to get current test case name

Post by xibinki » Tue Jul 30, 2019 9:59 am

Stub wrote:
Tue Jul 30, 2019 8:26 am
Are you running just your code module where you call TestSuite.CurrentTestContainer? Ie the big "Run Code" button from the code editor? If so then there simply is no Test Suite at that point. You have to run your test from the Test Suite using the Run button, or Run Selected Smart Folder/Run Selected Test Case via the right-click context menu.

Nowadays I code any code module that tries to access the TestSuite for whatever reason to anticipate that it's being run directly.
Yes, when I run from the "RUN CODE" button, I get a NULL value for the "TestSuite.CurrentTestContainer".
But if I run from the "RUN" button or the "Run selected smart folder", I get the name of my test case.

You said: "Nowadays I code any code module that tries to access the TestSuite for whatever reason to anticipate that it's being run directly.", how do you do that exactly? From what I understood, your modules are prepared to get information and values from the TestSuite while running only the "RUN CODE".

Also, is there a way to prepare my module code when executed from "RUN CODE" to get the name of the module code and TestSuite information (Ie: I have a UtestProcessBO_0003.cs module code and I what to retrieve the name of that file while running only the module code)?

User avatar
Stub
Posts: 515
Joined: Fri Jul 15, 2016 1:35 pm

Re: How to get current test case name

Post by Stub » Tue Jul 30, 2019 2:17 pm

My code modules that use the current Test Suite are coded defensively to detect when there is no Test Suite available. I just test for null as you've found. This avoids the code going horribly wrong. They just assume a default result because it's obvious they're being run in a sort of debug mode. Simples!

There might be a C# way of getting the current filename, like the __FILE__ preprocessor directive in C++, but I don't know what that trick is in .NET off the top of my head.