从基类的虚方法访问TestContext时,它为null

时间:2013-11-20 11:57:11

标签: c# automated-tests mstest ui-testing

我有一个基类ScriptBase,它有一个名为MyTestInitialize()的虚函数。当我从派生类调用MyTestInitialize()时,testContextInstance的值为null。 这有什么解决方案吗?请帮忙,因为我是自动化测试的新手。 在此先感谢

[CodedUITest]
public class ScriptsBase
{
    public ScriptsBase()
    {   
    }

    private static TestContext bingTestContext;

    public static TestContext BingTestContext
    {
        get { return ScriptsBase.bingTestContext; }
        set { ScriptsBase.bingTestContext = value;}
    }

    #region TestInitialize
    //Use TestInitialize to run code before running each test 
    [TestInitialize()]
    public virtual void MyTestInitialize()
    {
        Browser.CloseAllBrowsers();
        BingTestContext = testContextInstance;
    }
    #endregion

    #region TestCleanup
    //Use TestCleanup to run code after each test has run
    [TestCleanup()]
    public virtual void MyTestCleanup()
    {
        PPI.HomePage = new HomePageUI();
        Browser.CloseAllBrowsers();
    }
    #endregion

    #region TestContext
    /// <summary>
    ///Gets or sets the test context which provides
    ///information about and functionality for the current test run.
    ///</summary>
    public TestContext TestContext
    {
        get
        {
            return testContextInstance;
        }
        set
        {
            testContextInstance = value;
        }
    }
    private TestContext testContextInstance;
    #endregion
}

 public class DestinationMasterTestScripts : ScriptsBase
  {
       public DestinationMasterTestScripts()
       {      
       }

       [TestInitialize()]
       public override void MyTestInitialize()
       {
           Console.WriteLine("Initialize");
           base.MyTestInitialize();
       }
   }     

5 个答案:

答案 0 :(得分:4)

尝试创建ClassInitialize方法:

    private static TestContext bingTestContext

    [ClassInitialize]
    public static void ClassInit(TestContext con)
    {
      bingTestContext = con;
    }

答案 1 :(得分:3)

另一种选择是在基类中将TestContext声明为抽象

public abstract TestContext TestContext { get; set; }

在你派生的最具体的类中重写它

public override TestContext TestContext { get; set; }

答案 2 :(得分:0)

看看这是否有帮助,当我使用派生类1设置基类TestContext时,我发现它有效。

public TestContext TestContext
{
    get
    {
        return testContextInstance;
    }
    set
    {
        base.TestContext = value;
        testContextInstance = value;
    }
}

private TestContext testContextInstance;

答案 3 :(得分:0)

您应该为AssertTestContext使用相同的类,例如:

using Assert = NUnit.Framework.Assert;

using TestContext = NUnit.Framework.TestContext;

答案 4 :(得分:0)

不确定自发布此问题以来的6年内是否有所改变,但这对我来说几乎是不变的。将[TestClass]添加到派生类中,即可正确设置TestContext。也完全不需要BingTestContext。只需使用派生类中的this.TestContext。

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

public class ScriptsBase
{
    public static TestContext BingTestContext { get; set; }

    public TestContext TestContext { get; set; }

    [TestInitialize]
    public virtual void MyTestInitialize()
    {
        BingTestContext = this.TestContext;
    }

    [TestCleanup]
    public virtual void MyTestCleanup()
    {
    }
}

[TestClass]
public class DestinationMasterTestScripts : ScriptsBase
{
    [TestInitialize]
    public override void MyTestInitialize()
    {
        base.MyTestInitialize();
    }

    [TestMethod]
    public void Foo()
    {
        Console.WriteLine(this.TestContext);
    }
}