Passing Class Object between Test Cases

Best practices, code snippets for common functionality, examples, and guidelines.
shanefox
Posts: 8
Joined: Tue Jul 10, 2018 8:10 pm

Passing Class Object between Test Cases

Post by shanefox » Tue Jul 24, 2018 10:16 pm

Is it possible to pass a class object between test cases?

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

Re: Passing Class Object between Test Cases

Post by Stub » Wed Jul 25, 2018 9:04 am

When I want to pass parameters between test cases - e.g. some kind of filename - I sometimes return a string value via one of the module parameters (so I'm using the module parameter as an output parameter), then feed that returned value into another test case (this time as an input parameter).

Off the top of my head, if I wanted to reference a more complex object I think I might use the string parameter that I'm passing around as a key into some kind of collection e.g. a dictionary.

McTurtle
Posts: 297
Joined: Thu Feb 23, 2017 10:37 am
Location: Benedikt, Slovenia

Re: Passing Class Object between Test Cases

Post by McTurtle » Wed Jul 25, 2018 2:41 pm

Hello shanefox,

IMHO, I think that a singleton might be useful for you?
What if you create a class like this?
using System;

namespace SharingAClass
{
	public class myClass
	{
		static myClass _instance= new myClass();
		public static myClass Instance
		{
			get
			{
				return _instance;
			}
		}
		myClass()
		{
		}
		public string myVariable_one="";
		public string myVariable_two="";
	}
}
You should now be able to set and read the values from that class from anywhere in the test suite. In some random test case you could set myClass.Instance.myVariable_one="Test_one", in another one you could set myClass.Instance.myVariable_two="Test_two"; without having to declare a new instance of the class. Then again, somewhere completely different you can access them:
Report.Info(myClass.Instance.myVariable_one);
Report.Info(myClass.Instance.myVariable_two);
Is this useful to you?

Regards,
McTurtle

shanefox
Posts: 8
Joined: Tue Jul 10, 2018 8:10 pm

Re: Passing Class Object between Test Cases

Post by shanefox » Tue Aug 07, 2018 9:01 pm

Thanks. I will try it.