2

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

jsp
  • 2,546
  • 5
  • 36
  • 63

3 Answers3

7

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.

Don Kirkby
  • 53,582
  • 27
  • 205
  • 286
  • I had initially proposed that ...but the problem lies in the way two developers can write different test for the same kind of logic. Therefore want to write a wrapper function that would do things like check if the database credentials, if update/insert was successful or not.. – jsp Dec 29 '10 at 22:20
  • 3
    It sounds like what you are explaining is integration testing, it and unit testing are two very different beasts. We had a suite of integration tests that done the trick for us in my last job, we used the setup and teardown methods to wrap a transaction around any DB code that could be rolled back, if you need more detail I would be happy to help. – Burt Dec 29 '10 at 22:26
  • Yes, that is one of the attributes i was planning to use. I would be interested in learning more about using that. – jsp Dec 29 '10 at 22:36
  • 1
    Have a look at this post it is similar to how we performed the integration tests and worked quite well: http://stackoverflow.com/questions/321180/how-do-i-test-database-related-code-with-nunit – Burt Dec 29 '10 at 23:03
  • For a more general introduction, @Anonymous, you might enjoy Martin Fowler's article on evolutionary database design: http://www.martinfowler.com/articles/evodb.html For a specific tool, you might find dbUnit.NET useful: http://dbunit-net.sourceforge.net/ I haven't tried it, but I used the Java version successfully. – Don Kirkby Dec 29 '10 at 23:07
3

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/

Burt
  • 7,680
  • 18
  • 71
  • 127
1

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.

Rune Grimstad
  • 35,612
  • 10
  • 61
  • 76