5

Background

I am fairly new to unit testing, and have been recently using the Visual Studio Test Manager to create my Unit Tests. The way that I have currently been doing things is as follows:

  1. Create some libraries.
  2. Add the libraries to one big solution, along with the application.
  3. Create a folder called Tests that I put my Tests project into.
  4. Write unit tests for my libraries and stuff them all into the same folder called "Tests".

I run all of my tests, and this method seems to work for me. The reason I do it this way is that I sometimes feel I cannot create any practical tests unless all of the classes necessary for the application are actually present/involved in my solution.

Question

Should I be creating tests as I create the libraries?

I don't see the practicality, and it seems to just create more overhead getting the work done because I would have to create tests again for my application anyway.

11
  • Are these libraries that you wrote or libraries like Entity Framework? Commented Apr 21, 2016 at 19:06
  • @WindRaven: 1. Create some libraries. Commented Apr 21, 2016 at 19:06
  • @WindRaven Just DLLs generated from selecting "class library".
    – Snoop
    Commented Apr 21, 2016 at 19:07
  • The TDD'ers would be saying "Create your tests before you create your libraries." Commented Apr 21, 2016 at 19:07
  • In any case, how do you verify that your libraries work, independent of the applications they are running in? Commented Apr 21, 2016 at 19:08

2 Answers 2

5

You should be creating tests for all the code that you write, regardless of whether they're libraries or your application. Why ?

  1. you can assert that the code you write works
  2. the tests assert that that code continues to work as you change or add subsequent code.

In short, it doesn't matter whether it's a library or an application that you're writing code for.

1
  • 2
    However, lib's unit tests should be implemented and performed in the lib project.And lib can be released if only if its test ends successfuly If we were forced to do test of every lib in our projects we would need ages just for test all the code. :-)
    – Laiv
    Commented Apr 22, 2016 at 13:58
0

Once you have a rough idea of what a class or function should do, you have enough information to write tests. These tests may fail, due to the thing you're testing being broken, but that is essentially a good thing.

If you prefer writing the code before you write the tests for the code (sometimes, having an implementation may yield a better idea of what data needs to be passed), you should still aim at writing the code once the class or function has been finished, not wait until the whole library is done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.