11

我有一个基类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();
       }
   }     
4

5 回答 5

7

尝试创建一个ClassInitialize方法:

    private static TestContext bingTestContext

    [ClassInitialize]
    public static void ClassInit(TestContext con)
    {
      bingTestContext = con;
    }
于 2013-11-20T12:34:31.423 回答
5

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

public abstract TestContext TestContext { get; set; }

并在您最派生的具体类中覆盖它

public override TestContext TestContext { get; set; }
于 2014-01-29T22:34:55.310 回答
1

不确定自发布此问题以来的 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);
    }
}
于 2020-01-08T22:00:33.347 回答
0

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

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

private TestContext testContextInstance;
于 2014-10-16T08:55:58.227 回答
0

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

using Assert = NUnit.Framework.Assert;

using TestContext = NUnit.Framework.TestContext;
于 2015-11-01T02:56:58.083 回答