Create a Utility Class in Standalone version

I would like to create global variables and/or functions in Test Studio Standalone version. These variables/functions need to be accessible from all the tests within the test project.

Solution

Define a utility class that will contain the intended functions/variables. This is very easy to do when you're working with the Visual Studio plugin. It's a bit trickier in the Standalone version, however.

  1. Create a new test.
  2. Add a coded step.

    • This automatically generates a new class that has the same name as the test (this is the code-behind file).
  1. In the same namespace we're going to manually add a brand new class.

    • This will be the Utilities class. Click the View Entire Code Behind File button to display all of the code for the namespace (the project):

    Code behind

  1. Add the Utility class within the project's namespace.

    • The namespace already contains one class which was generated from the coded step of the test.
  1. Write your class definition in the namespace. Ensure it's within the namespace, but not inside of another class.

Let's say I want to define a class that has one integer variable and one function which writes text to a .txt file. Here's what the class definition looks like:

public static class Utility
{  
     public  static int valueHolder;
     public static void writeToFile(String path,String content)
          {
          // Write the string to a file.
          System.IO.StreamWriter file = new System.IO.StreamWriter(path);
          file.WriteLine(content);
          file.Close();
          }
}
Public NotInheritable Class Utility
    Private Sub New()
    End Sub

    Public Shared valueHolder As Integer
    Public Shared Sub writeToFile(path As [String], content As [String])


        Dim file As New System.IO.StreamWriter(path)
        file.WriteLine(content)
        file.Close()
    End Sub
End Class

Run the test once. This builds the assembly. Then you'll be able to access the functions/variables within your Utilities class from all tests in this project. For instance, if you're using the above class definition, you can acess it like so:

Utility.valueHolder = 2; //Set value
Log.WriteLine("Value from Utility class:" + Utility.valueHolder.ToString()); //Write value to test Log
Utility.writeToFile(@"c:\myNewText200.txt", "some content"); //Invoke function
Utility.valueHolder = 2
'Set value 
Log.WriteLine("Value from Utility class:" + Utility.valueHolder.ToString())
'Write value to test Log 
Utility.writeToFile("c:\myNewText200.txt", "some content")
'Invoke function

Note that the Test Studio code editor will not autocomplete references to the contents of the utility class made in other files in the project.