Sharing static class variables data between two Selenium classes c#



  • 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?



  • 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.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2