Search code examples
c#visual-studiounit-testingtestingfixtures

How to implement testing fixtures using Visual Studio and C#


I am new to C# and I and using the built in Test support in Visual Studio 2012 to employ test driven development for a new library. I have implemented a few test cases for a USB SPI adapter and find myself duplicating alot of "startup" and "teardown" code for each test case. I am used to python pytest with its 'fixtures' to help reduce duplicated test code. So far I have been unable to find a similar concept with C# and Visual Studio.

Consider the following where multiple test cases have to list and close the SPI port each time a test is run. How can I create the equivalent of a 'fixture' where I write this startup and teardown code once to be run with each test case? Thanks.

namespace CheetahTestSuite
{
    [TestClass]
    public class CheetahTest
    {

        [TestMethod]
        public void TestOpenPort()
        {

            // Ask the system for the Cheetah devices found on the system
            CheetahSPI spi = new CheetahSPI();
            string found_devices = spi.ListDevices("not used");

            ...

            // Close the port
            string close_success = spi.ClosePort();
            Assert.AreEqual(close_success, "0,OK");

        }

        [TestMethod]
        public void TestConfigureSPIPort()
        {

            // Ask the system for the Cheetah devices found on the system
            CheetahSPI spi = new CheetahSPI();
            string found_devices = spi.ListDevices("not used");

            ....

            // Close the port
            string close_success = spi.ClosePort();
            Assert.AreEqual(close_success, "0,OK");

        }

    }

}

I tried to research fixtures with C# and Visual Studio. I expected a way to implement 'fixtures' to reduce duplicated code.


Solution

  • You can open the SPI port in the TestInitialize method and close it in the TestCleanup method. This eliminates the need to manually write these boot and teardown codes in each test method.

    Here's an example of how to do it:

    namespace CheetahTestSuite
    {
        [TestClass]
        public class CheetahTest
        {
            private CheetahSPI spi;
            [TestInitialize]
            public void Setup()
            {
                spi = new CheetahSPI();
                string found_devices = spi.ListDevices("not used");
            }
    
            [TestCleanup]
            public void Teardown()
            {
                // Close the port
                string close_success = spi.ClosePort();
                Assert.AreEqual(close_success, "0,OK");
            }
    
            [TestMethod]
            public void TestOpenPort()
            {
                // Your test code
                Assert.IsNotNull(spi);
            }
    
            [TestMethod]
            public void TestConfigureSPIPort()
            {
                // Your test code
                Assert.IsNotNull(spi);
            }
        }
    }