Test Strategy Consultation

So, I am becoming more and more absorbed in test-based development, and the more code I write thinking about tdd, the more solutions it seems to me, as if I were to evaluate the degree of testing that I have to write. I would like to set a personal policy regarding how many testing units I should write for my own projects, and I was wondering if I would get some tips on how you feel about what you all do.

Here is an example of a solution that I am currently facing ...

I have three classes ...

public class User { public string Username { get; set; } public List<Favorite> Favorties { get; set; } } public class Favorite { public string Username { get; set; } public int rank { get; set; } } public class UserManager { public List<Favorite> GetTop5(User user) { var qry = from fav in user.Favorties.OrderBy(f => f.rank) select fav; return qry.Take<Favorite>(5).ToList(); } } 

I have a data access level for the User class, for which I already have a GetUser test setup. As you can see, in my business logic, I have a UserManager.GetTop5 () method that returns the 5 best favorites for the User that I just pulled from db. This method is very simple and does not currently contain any external resources or dependencies.

So my question is: would you go further and write another test for this "GetTop5" function, although you have very little chance of failure?

Do you set up the test anyway if you continue to function in the future? Or do you think the test is excessive here?

+4
source share
5 answers

You wonโ€™t believe it, but here is what Kent Beck said right here in Stackoverflow:

โ€œI get paid for code that works, not tests, so my philosophy is to test as little as possible in order to achieve a certain level of trust (I suspect this level of trust is high compared to industry standards, but it may just be arrogant). Unless I usually make a mistake (for example, setting the wrong variables in the constructor), I donโ€™t check it. "

Link: Link:

+5
source

when doing TDD, I write at least one unit test for each function.

So, is there a GetTop5 function? if so, he deserves a test. If this is not a feature, then it should not exist; -)

+3
source

A good point, followed by writing a test! If you first implement functions and then test them, this seems strange. This is why you should write tests first and then implement functions. Moreover, it will make you think about how you want to use your functions. In addition, if you first implement everything that you want to implement, you will find yourself in situations where the code is difficult to verify in any case. And again, the first approach to testing helps in this, the code will be more testable if you start by implementing tests before actually implementing your functions.

+3
source

Yes, I would also write a test for this method, in general, you should have a test for the function that you are implementing. Remember that TDD is not only for testing, if this method works, you should also check how it handles exceptions, for example, what happens if it receives Null as a custom object.

When developing using TDD, you are really developing an API that other members of your team will have to use, so TDD allows you to write how you want the API to be used, this means how it is called and how errors are handled. For more complex methods, you may need to return your own exception in order to make it clear what went wrong.

+3
source

I am testing most things.

Whenever I write tests, I also see tests as documentation or instructions on how my code should be used, for me and others to read in the future.

I am not testing the implementation. I want to be able to change the implementation without changing my tests.

I used TDD, maybe a year or two, so maybe I will mature and stop. So far I am still participating and I think that I am not writing enough tests.

0
source

All Articles