0

I have two Selenium C# test case classes, ClassA and ClassB. I would like to reuse a static variable VarA, defined in another static non-test ClassC. After ClassA has made a change to the variable, I would like that when test methods in ClassB launch to use the new VarA value.

My question is, having two [TestClass] in C# (ClassA and ClassB), how can one use a static variable from a non-test static class to share data between the two tests classes?

0

The could both inherit from the same class which could set and get data using public class methods. To keep this safe make that data structure independent from other non class methods and copy into it by value.

0

I suppose it depends on how they are being ran but generally speaking so long as you are not tearing down your AppDomain I believe it should keep your variable across the tests if it is in a static class.

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace PaulTest
{
  [TestClass]
  public class ClassA
  {
    [TestMethod]
    public void ClassATest()
    {
      ClassC.VarA = "Test";
      Assert.AreEqual("Test", ClassC.VarA);
    }
  }
  [TestClass]
  public class ClassB
  {
    [TestMethod]
    public void ClassBTest()
    {
      Assert.AreEqual("Test", ClassC.VarA);
    }
  }
  public static class ClassC
  {
    public static string VarA { get; set; }
  }
}

In this the tests are passing using the built in VS Test framework when running them all together.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.