Reputation:
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?
Upvotes: 6
Views: 8592
Reputation: 31
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);
}
}
}
Upvotes: 0
Reputation: 3119
If you're really interested in being rigorous, there are a few parts to this.
Upvotes: 2
Reputation: 41266
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.
Upvotes: 2
Reputation: 67135
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"));
}
Upvotes: 4