0

I want to compare the current test method name, but getting an exception that TestContext returns null.

public TestContext TestContext { get; set; }

private void SetTaxCode(Bene beneDetails)
{if (TestContext.TestName.Contains("NegativeTest"))
{SetBenecode(TestDataProvider.TestDataProvider.InValidBeneficiaryTaxCode);
}
else
SetBenecode(beneDetails.BeneficiaryTaxCode);
}

TestContext.TestName returns correct test name in the basic.cs, but I can't inherit basic.cs to the new class, how can I then use TestContext in different classes?

Gkm
  • 237
  • 1
  • 5
  • 19

2 Answers2

0

You could make your TestContext property static. That way, you will be able to call it even without instantiating the basic.cs class.

If basic.cs is not accessible to other classes, you could make a static class and put your TestContext property there.

PixelPlex
  • 749
  • 5
  • 21
0

As far as I know from experience with CodedUI, TestContext cannot be made static. It's must be non-static by definition. If you want to use TestContext in different classes you can pass it in as a parameter similar to the following:

[CodedUITest]
    public class SomeCodedUITest1
    {    
            public TestContext TestContext
            {
                get
                {
                    return testContextInstance;
                }
                set
                {
                    testContextInstance = value;
                }
            }
            private TestContext testContextInstance;

        [TestCategory("DataFiles"),
                 DataSource ( .... )]
          public void SampleMethod()
            {
                MyClass = new MyClass(TestContext);
            }
    }

Then inside MyClass class just use constructor

public MyClass (TestContext testContext)
  {
  }

You can then use TestContext data inside MyClass. For example, testContext.DataConnection.Database.

Ivan B
  • 84
  • 8