-1

I want to know if it's possible to somehow call one functional test inside another, and have the parent functional test be able to see if the child tests passes or not ? ... Please note that the parent and child tests are in different projects and although I can reference the child test's project in my project's CSPROJ file for read access, I cannot modify anything in the child test's project.

Something like this is what I'm after:

[TestClass]
public class ParentTests
{

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

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

... where Test_Child is the name of the child test method I want to run within my parent test.

I know very clearly that this is not the recommended testing methodology to use as tests need to be stateless, independent, modular with consistent results, however I have a very tricky scenario which requires me to do what I've stated above - there's no way around it. As I said, I do not have control over the child functional test source code, so if the above isn't possible then I'll have to copy paste the entire code of the child test, into my parent test, which I don't want to do as it's quite big and involves importing several classes, interfaces, etc., and I don't have working knowledge of that code.

In case if you still want to know why I'm asking this, please put a comment and I can answer.

Ahmad
  • 12,886
  • 30
  • 93
  • 146

1 Answers1

0

Well, i have no idea, why do you need things like this, but any Assert Method throws an AssertFailedException.

So your code should look like this:

[TestClass]
public class ChildTests
{

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

and the parent tests:

[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
        }
    }
}
Vadim
  • 471
  • 6
  • 15
  • So I cannot change the child test's code and add `Assert.Fail` .. The child test is a normal test and has stuff like `Assert.IsTrue`, `Assert.AreEqual`, etc .. – Ahmad Jun 04 '20 at 17:54
  • You shouldn't add ```Assert.Fail```. Each function from Assert-Family throws ```AssertFailedException``` when it fails (```Assert.IsTrue```, ```Assert.AreEqual```, etc ..) – Vadim Jun 05 '20 at 05:52