Question

HI, I have been assigned a task of exploring unit testing using tools available in the market. My question is how do i write an API that would help in writing unit test cases. Example i can use nunit to write something like this to check whether file exists in the given location.

<Test()> _
   Public Sub CheckOutputFileInfo()
    ReportPath = "D:temp\test.txt"
    Dim result As Boolean
    result = File.Exists(ReportPath)
    Assert.IsTrue(result)
End Sub

I understand this is not the best example but my question is how do i incorporate nunit and develop an API so that other developers/testers can write test cases without bothering about to learn about nunit. FYI I have never written an API this will be my first shot at it. Any recommendations on where to start?? thanks

Was it helpful?

Solution

Your example isn't strictly a unit test as it hits the file system.

A test is not a unit test if:

  1. It talks to the database
  2. It communicates across the network
  3. It touches the file system
  4. It can't run at the same time as any of your other unit tests
  5. You have to do special things to your environment (such as editing config files) to run it.

I would advise you learn Unit Test best practices and patterns before starting to try to make your developers start unit testing. From experience someone needs to champion unit testing having fully grasped it themselves. It will save you a lot of headaches in the long run.

Here is an excellent book to get you started:

http://artofunittesting.com/

OTHER TIPS

I think you're better off letting the developers use nUnit. It's already nicely designed and flexible. If you want to make life easier for your developers, try building some helper classes that set up test objects and sample data in configurations that are needed by many different tests. Maybe try something like the Creation Method pattern. That's from a really good book called xUnit Test Patterns that describes lots of ways to make test code easier to write, read, and maintain. Most of the book is available online, and the Brief Tour is a good place to start.

Why would you want to do that? NUnit and most alternatives are really simple to learn and use. The challenge in writing unit tests is not mastering the api, but rather to write good tests and testable code.

To use NUnit efficiently you only need to know the Test and TestFixture and SetUp attributes and to know the available asserts in the Assert class. It is really simple!

So I would recommend you focus on how to write good tests and how to write code that is easy to test. Both topics are challenging and there is much to learn.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top