简体   繁体   中英

Testing a Class Library in C#

This is a very vague (and noob) question, but.....How would one approach testing a class library in C#? I am using nUnit for testing.

What I am trying to do is test database interaction. The input will be a serialized XML object, deserialize it to test it against the code, then the XML object will be re-serialized and outputted.

Hopefully, this gives some insight. I had thought about creating a test app that creates an instance of the library. Is there a different/better/more efficient approach I can take?

You have to create a separate project that uses NUnit's data annotations ( TestFixture , Test , etc). Then you can build that project and load the created DLL into Nunit.

As far as the tests, just write them as you would normally ( Arrange-Act-Assert is the more prevalent pattern )

Something like this

[Test]
public void MethodName_CallDatabase_ObjectDeserialized()
{
    //Arrange
    var db = new db();
    //Act 
    var output = db.ExecuteCall();
    //Assert
    Assert.That(output, Is.EqualTo("123"));
}

As for the how, typically your solution will look like this:

 Solution (Your Application)
    + YourApplication.Library
    + YourApplication.WebApp
    + YourApplication.Tests

The Tests project is a specific project you can add to your solution. In that, you can make Unit Test files which will use the NUnit DLL to mark them up and then run. That means marking those classes with TestFixture and specific methods in those classes as Test methods, and then they execute parts of your YourApplication.Library project with supporting Assert calls to verify the results of the library calls.

If you're really interested in being rigorous, there are a few parts to this.

  1. Familiarize yourself with NUnit, MBUnit, or MSTest
  2. Figure out where your library interacts with other libraries, frameworks, tools. Write some test cases that codify your assumptions about how those other things should behave (and how they should misbehave). Install and familiarize yourself with a mock/stub library, like Moq or Rhino.Mocks. These will tell you when your code is interacting with external code in a way that is not correct.
  3. If you're feeling up to it, write some more tests (these straddle the line between integration and unit tests, IMO) that connect a real-but-simple database with the same or similar schema, like SQLite. Now your code is no longer talking with mocks that you've pre-programmed with your expectations, but it's also not reaching out to a full-on database.
  4. Integration tests. Hook that thing up to a real [not-production] database.

Use one of the many existing unit test frameworks for this.

nUnit , xUnit or even MSTest (which comes with many editions of Visual Studio).

If you dont want to create a new test project, just add a new class in the existing class library project and add Nunit annotation TestFixture to it and have tests as methods inside the same. as follows

namespace APIWorkOut
{
    [TestFixture]
    public class TestClass
    {
        [Test]
        public void getListOfUser()
        {
            API api = new API();
            var listOFUsers = api.getUsers();
            Console.Write(listOFUsers.Data.Count);
        }       
        
    }
}

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