Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test writing strategy advice

So, I'm getting more and more engulfed with test driven development these days and the more code I write while thinking tdd, the more decisions it's seems as though I have to make about the extent of testing that I should write. I would like to set kind of a personal policy as to how much unit testing I should write for my own projects, and was wondering if I get some advice as to how what kind of you approach you all take.

Here an example of a decision that I'm 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 layer for the User class for which I already have a "GetUser" test setup. As you can see, in my business logic I have a method UserManager.GetTop5() that returns the top 5 favorites for the User that I just pulled out of the db. This method is very simple and currently involves NO external resources or dependencies.

So my question is would you go ahead and write another test for this "GetTop5" function point, even though there's very little chance for failure?

Do you setup a test anyway in case you extend upon the functionality in the future? Or do you think that a test here is excessive?

like image 221
matt_dev Avatar asked Dec 09 '22 21:12

matt_dev


2 Answers

You won't believe this, but here's what Kent Beck had to say, right here on StackOverflow:

"I get paid for code that works, not for tests, so my philosophy is to test as little as possible to reach a given level of confidence (I suspect this level of confidence is high compared to industry standards, but that could just be hubris). If I don't typically make a kind of mistake (like setting the wrong variables in a constructor), I don't test for it. "

Link: Link:

like image 198
Charlie Flowers Avatar answered Dec 14 '22 23:12

Charlie Flowers


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

so, is GetTop5 a feature? if so, it deserves a test. If it's not a feature, then it doesn't need to exist ;-)

like image 23
Steven A. Lowe Avatar answered Dec 15 '22 01:12

Steven A. Lowe