skip to Main Content

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.

2

Answers


  1. To use common initalization and deinitialization code, use the constructor and the Dispose method:

    [TestClass]
    public sealed class CheetahTest : IDisposable
    {
        private CheethSpi _testee;
        public CheetahTest()
        {
            _testee = new CheetahSPI();
            string found_devices = _testee.ListDevices("not used");
            // ... more init code
        }
        
        [TestMethod]
        public void Foo()
        {
            _testee.DoSomeTest();
        }
    
        public void Dispose()
        {
            _testee.Dispose();
        }
    }
    

    Note that in this case, the Init/Dispose code will still run for each test, but you have to write it only once. There are ways to write a fixture helper that runs only once per class, but that’s a bit more tricky.

    Login or Signup to reply.
  2. 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);
            }
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search