Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Unit Testing with TestInitialize/TestCleanup in base class

I am testing a module where every test class share the same behavior:

  • Begin a transaction
  • Execute SQL queries
  • Rollback transaction

I've decided to use TestInitialize and TestCleanup to execute the Begin and Rollback of transactions respectively.

The strait forward approach would be writing the TestInitialize/TestCleanup in a parent class but that is not going to work with this testing framework.

The work around for this was to use partial classes. This approach seems to viable in my case because my test classes are mainly stateless. Event not being the ideal solution it at least saved me a couple of copy/paste actions.

Anyone knows a better way to do this?

Here is a sample of the partial class solution:

In my case I test each module separately and for this example I will use the Sales module:

SalesTest.cs file:

[TestClass]
public partial class SalesTest
{
    [TestInitialize]
    public void Setup()
    {
        //begin transaction
    }

    [TestCleanup]
    public void Cleanup()
    {
        //rollback transaction
    }
}

SalesTest.Order file:

public partial class SalesTest
{
    [TestMethod]
    public void SaveOrder_OnlyRequiredValuesFilled_SuccessfullySaved()
    {
        //Run some SQL queries 
    }
}
like image 363
Daniel Teleginski Camargo Avatar asked Sep 03 '15 18:09

Daniel Teleginski Camargo


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C full form?

Full form of C is “COMPILE”.

What is C language basics?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

Looks like you're using Microsoft.VisualStudio.TestTools.UnitTesting framework. I don't see any problem using a base class for Cleanup and Initialize.

E.g.

   [TestClass]
    public class TestDemo : BaseTests
    {
        [TestMethod]
        public void SaveOrder_OnlyRequiredValuesFilled_SuccessfullySaved()
        {
            //Run some SQL queries 
        }

    }

    [TestClass]
    public abstract class BaseTests
    {
        [TestInitialize]
        public void Setup()
        {
            Console.WriteLine("Setup executed.");
            //begin transaction
        }

        [TestCleanup]
        public void Cleanup()
        {
            Console.WriteLine("Cleanup executed.");
            //rollback transaction
        }
    }

This will work fine and I can inherit the BaseTest to any Test and the Intiailize and Cleanup will execute before and after any test.

like image 138
vendettamit Avatar answered Oct 06 '22 11:10

vendettamit