4

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.

Snoop
  • 2,718
  • 5
  • 24
  • 52
  • Are these libraries that you wrote or libraries like Entity Framework? – RubberChickenLeader Apr 21 '16 at 19:06
  • @WindRaven: 1. Create some libraries. – Robert Harvey Apr 21 '16 at 19:06
  • @WindRaven Just DLLs generated from selecting "class library". – Snoop Apr 21 '16 at 19:07
  • The TDD'ers would be saying "Create your tests *before* you create your libraries." – Robert Harvey Apr 21 '16 at 19:07
  • In any case, how do you verify that your libraries work, *independent of the applications they are running in?* – Robert Harvey Apr 21 '16 at 19:08
  • @RobertHarvey In a way that does make sense, because unless you know what problem you're trying to solve, I guess there's no sense in creating a library for no reason. – Snoop Apr 21 '16 at 19:08
  • @RobertHarvey Also... What is TDD? – Snoop Apr 21 '16 at 19:19
  • Are these application specific libraries? If so, does it really matter? – JeffO Apr 21 '16 at 19:29
  • @JeffO you know what, they are actually used for many different applications. – Snoop Apr 21 '16 at 19:42
  • writing for libraries is more important than for application: "Main reason why I prefer test first is it helps me understand how to design my code so that it's convenient to use. I am not good at imagining in my head how my module will be used by other modules so I can do it overly complicated and that will bite me later and possibly even require redesign..." ([Why is it often said that the test cases need to be made before we start coding?](http://programmers.stackexchange.com/a/182329/31260)) – gnat Apr 21 '16 at 19:58
  • The tests for the libraries should be placed into their packages, not into the application which uses the libraries. By doing that you ensure the libraries internals work and the logic of the library is reliable no matter who uses it. – Andy Apr 22 '16 at 07:55

2 Answers2

4

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.

Brian Agnew
  • 4,676
  • 24
  • 19
  • 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 Apr 22 '16 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.

Vatine
  • 4,251
  • 21
  • 20