Search code examples
c#.netunit-testingtddxunit

Best practice for unit test cases


I am using the xUnit.net test framework and in each unit test I have certain steps which I am doing in each case. I would like to know if there is a way I call this method once before my unit case starts and also call when all unit test cases has been executed.

For example: In the scenario below I have two unit cases and in each case I am creating a local DB, populating it with data and then running my test and once it is done I am calling method to delete the DB. This I am doing in each test case. Instead of multiple creation I would like to create once and populate once and then delete db once all test case has been executed. It is important for me to delete what I have created as the test cases has certain cases which will fail if Database is not created when the tests are executed.

[Fact]
public void UnitCase1()
{
   CreateDb();
   UploadData();
   ...//My set of operation to test this case
   ...//Assert
   DeleteDb()
}

[Fact]
public void UnitCase2()
{
   CreateDb();
   UploadData();
   ...//My set of operation to test this case
   ...//Assert
   DeleteDb()
}

Editing after Answer from Eric:(I tried but its not working)

public class CosmosDataFixture : IDisposable
    {
        public static readonly string CosmosEndpoint = "https://localhost:8081";
        public static readonly string EmulatorKey = "Mykey";
        public static readonly string DatabaseId = "Databasename";
        public static readonly string RecordingCollection = "collectionName";
        string Root = Directory.GetParent( Directory.GetCurrentDirectory() ).Parent.Parent.FullName;
        DocumentClient client = null;

        public void ReadAllData( DocumentClient client )
        {
           //reading document code
        }

        public void ReadConfigAsync()
        {
            client = new DocumentClient( new Uri( CosmosEndpoint ), EmulatorKey,
                 new ConnectionPolicy
                 {
                     ConnectionMode = ConnectionMode.Direct,
                     ConnectionProtocol = Protocol.Tcp

                 } );
        }
 public void CreateDatabase()
        {// create db code
        }
private void DeleteDatabase()
        {
          // delete db code
        }
     public CosmosDataFixture()
        {
            ReadConfigAsync();
            CreateDatabase();
            ReadAllData( client );

        }

        public void Dispose()
        {
            DeleteDatabase();
        }
    }
public class CosmosDataTests : IClassFixture<CosmosDataFixture>
    {
        CosmosDataFixture fixture;

        public CosmosDataTests( CosmosDataFixture fixture )
        {
            this.fixture = fixture;
        }

        [Fact]
        public async Task CheckDatabaseandCollectionCreation()
        {          
            List<string> collectionName = new List<string>();
            var uri = UriFactory.CreateDatabaseUri(DatabaseId);// don't get DatabaseId or client says does not exist in current context
            var collections = await client.ReadDocumentCollectionFeedAsync( uri );
            foreach( var collection in collections )
            {
                collectionName.Add( collection.Id);
            }
                
        }

Solution

  • That's what [SetUp] and [TearDown] are for in NUnit. They are run right before and right after each test case, respectively. In xUnit you would usually implement a default constructor and IDisposable.

    For example:

    
    public TestClass()
    {
       CreateDb();
       UploadData();
    }
    
    
    public void Dispose()
    {
       DeleteDb()
    }
    
    [Fact]
    public void UnitCase1()
    {
       ...//My set of operation to test this case
       ...//Assert
    }
    
    [Fact]
    public void UnitCase2()
    {
       ...//My set of operation to test this case
       ...//Assert
    }