【问题标题】:Any way to run one test inside another, and be able to see if child test passes?有什么方法可以在另一个测试中运行一个测试,并能够查看子测试是否通过?
【发布时间】:2020-06-04 03:10:24
【问题描述】:

我想知道是否有可能以某种方式在另一个功能测试中调用一个功能测试,并让父功能测试能够查看子测试是否通过? ...请注意,父测试和子测试位于不同的项目中,虽然我可以在项目的 CSPROJ 文件中引用子测试的项目以进行读取访问,但我无法修改子测试项目中的任何内容。

我想要的是这样的:

[TestClass]
public class ParentTests
{

    [TestMethod]
    public void Test_Parent()
    {
        var childTestPassed = RunAnotherTest(Test_Child);

        if(childTestRunResult == true)
        {
            // do something
        } else
        {
            // do something
        }
    }
}

...Test_Child 是我想在父测试中运行的子测试方法的名称。

我很清楚 这是 不是 推荐使用的测试方法,因为测试需要是无状态的、独立的、模块化的、结果一致的,但是我有一个非常棘手的场景,需要我做我上面所说的 - 有没有办法解决它。正如我所说,我无法控制子功能测试源代码,因此如果无法实现上述操作,那么我将不得不将子测试的整个代码复制粘贴到我的父测试中,而我没有我不想这样做,因为它很大并且涉及导入多个类、接口等,而且我没有该代码的工作知识。

如果您仍然想知道我为什么要问这个问题,请发表评论,我可以回答。

【问题讨论】:

  • 谁投了反对票,你能解释一下你为什么这么做吗?
  • 你试过你的代码了吗?它奏效了吗?没用吗?
  • 我的代码是伪代码,仅用于说明目的.. 没有称为RunAnotherTest 的方法..
  • 当然有 - 您刚刚将其命名为 Test_Child。这是另一个程序集中的方法。

标签: c# testing integration-testing mstest functional-testing


【解决方案1】:

好吧,我不知道,你为什么需要这样的东西,但是任何 Assert 方法都会抛出 AssertFailedException

所以你的代码应该是这样的:

[TestClass]
public class ChildTests
{

    [TestMethod]
    public void Test_Child()
    {
       Assert.Fail ("Child test failed.");
    }
}

和父测试:

[TestClass]
public class ParentTests
{

    [TestMethod]
    public void Test_Parent()
    {
        ChildTests childTests = new ChildTests();

        try
        {
            childTests.Test_Child();
            // child test succeded
        }
        catch (AssertFailedException ex) 
        {
            // child test failed
        }
    }
}

【讨论】:

  • 所以我不能更改子测试的代码并添加Assert.Fail .. 子测试是一个普通测试,有Assert.IsTrueAssert.AreEqual 等内容。
  • 您不应添加Assert.Fail。 Assert-Family 中的每个函数在失败时都会抛出 AssertFailedExceptionAssert.IsTrueAssert.AreEqual 等)
猜你喜欢
  • 2014-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多