简体   繁体   中英

Writing Xunit test cases in C#

I am kind of learning to write unit test cases and I am using Xunit framework. I have a scenario where I would like to write a test case to test different scenario in my cosmos db emulator. To do that I am trying to create a database, container and insert few test data in my cosmos db emulator and then write my facts and also delete it once test cases are completed...below is the code which I figured out from internet, would like to know if I am doing it correctly... and where can I start writing my test cases.

    namespace Project.Tests
{
    public class DatabaseFixture : IDisposable
    {
        private static readonly string CosmosEndpoint = "https://localhost:8081";
        private static readonly string EmulatorKey = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==";
        private static readonly string DatabaseId = "Recordings";
        private static readonly string RecordingCollection = "testdata";

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

            } );
        var databaseCreationResult = client.CreateDatabaseAsync( new Database { Id = DatabaseId } ).Result;
        var collectionCreationResult = client.CreateDocumentCollectionAsync( UriFactory.CreateDatabaseUri( DatabaseId ),
            new DocumentCollection { Id = RecordingCollection } ).Result;
        var recData = new Recordings { Id = "Test" };

        
        var itemResult = client
            .CreateDocumentAsync(
                UriFactory.CreateDocumentCollectionUri( DatabaseId, RecordingCollection ), recData )
            .Result;            
        var document = client
            .ReadDocumentAsync(
                UriFactory.CreateDocumentUri( DatabaseId, RecordingCollection, itemResult.Resource.Id ) )
            .Result;            
        Recordings site = (dynamic)document.Resource;
    }

    public void Dispose()
    {
        // ... clean up test data from the database ...
        throw new NotImplementedException();
    }
}

public class Recordings
{
    public string Id { get; set; }
}

public class MyDatabaseTests : IClassFixture<DatabaseFixture>
{
    DatabaseFixture fixture;

    public MyDatabaseTests( DatabaseFixture fixture )
    {
        this.fixture = fixture;
    }

    // ... write tests, using fixture.Db to get access to the database server ...
}

}

Be careful that using a web API is not really part of the Unit Test philosophy. A Unit Test is usually expected to be independent from external interaction.

You can still use xUnit to peform your testing, but you are not in a UNIT test context. If you have access to the code behind the service, you could Unit Test it without the Web layer. (as an exemple, you can Unit test directly the REST controller class.)

If you ignore this point, I think the response is already in your question.

You can directly write your tests in the test class.

public class MyDatabaseTests : IClassFixture<DatabaseFixture>
{
    DatabaseFixture fixture;

    public MyDatabaseTests( DatabaseFixture fixture )
    {
        this.fixture = fixture;
    }

    // Write test method here
    [Fact]
    private void MyTestMethod()
    {
        // Prepare Test
        /// Prepare your test data here.

        // Execute Test
        /// Execute your test operation here.

        // Validate Test
        ///  Use Assert methods here.
        /// Assert.True(....);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM