2

I have 3 projects. Call them project A, B and C. Those projects have very similar functionality even though they have nothing to do with each other. By researching on the internet I see people place their unit tests on the project they are working on.

Let me explain why I find it helpful to create one unit test project that test all the projects instead of 3 unit tests projects.

This is the pattern I am using:

    [Test]
    public void TestFoo()
    {
        var filter = SystemType.ProjectA | SystemType.ProjectC; // Need to test this on projectA and ProjectB

        // linuxSystems are the computers where I perform the tests
        var linuxSystems = Globals.LinuxSystemsToTest();

        foreach (var ls in linuxSystems)
        {
            // do not test if flag is not precent
            if (filter.HasFlag(ls.SystemType) == false)
                return;

            ls.Connect();

            var output = ls.RunCommand("sudo cat /var/someFile");

            Assert.IsTrue(output.Contains("foo"));
            // perform test
        }
    }

If I decide to create a Unit Test project per project (having 3) then I will have to create 2 unit tests calling the same base class. Moreover, when adding a new test if I need to test that on all 3 projects I will have to open the 3 projects and add the test on each project instead of just having the line var filet = SystemType.ProjectA | SystemType.ProjectB | SystemType.ProjectC;. Having one project will prevent me from having to do all that.

What will be the best pattern that you guys recommend for testing my 3 projects?

Tono Nam
  • 34,064
  • 78
  • 298
  • 470
  • 2
    Why do you not put all 3 project in the same solution if they're that similar? Also the code you shared does not look like a unit test it looks like a functional test. Having one project for functional tests is fine. Having one unit test project for multiple projects that do not depend on each other does not work out. This stackoverflow question will help explain the difference between unit and functional tests https://stackoverflow.com/questions/2741832/unit-tests-vs-functional-tests – Noremac Aug 15 '19 at 21:22
  • It's not clear what the relationship is between that filter and the test. Why does the filter refer to A and C but the comment says A and B? How does something in one project determine what should be tested in another project? Can you use parameterized tests or otherwise specify what *should* be tested instead of filtering some things out? – Scott Hannen Aug 15 '19 at 22:29
  • For example all 3 projects must tests that the dotnet core is installed @ScottHannen – Tono Nam Aug 15 '19 at 23:49
  • You should ALWAYS have one-to-one relationship between your Code-Under-Test and Test. That is, suppose you have a `Namespace.Company.Product` project; you should have a corresponding `Namespace.Company.Product.Tests` project. Within it, replicate exactly the folder structure and have the same classes with the word `Tests` added to the end. This makes finding the right file surprisingly easy, and everything is logical. If you have any common code to use for testing purposes only, create a fourth library and call it `Company.TestUtilities` (or better yet, make that a nuget package) – zaitsman Sep 19 '19 at 07:06

0 Answers0