Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it good practice to do unit test coverage for even plain classes

Tags:

Here is an example of an class with no behaviour at all. So the question is should I be doing unit test coverage for it, as I see it as unnecessary for it does have any behaviour in it.

public class Employee
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Address Address { get; set; }
}

My code coverage result complains that I have not done any coverage for the class

like image 200
HatSoft Avatar asked Oct 11 '12 18:10

HatSoft


People also ask

What is considered good unit test coverage?

Aim for 95% or higher coverage with unit tests for new application code. When developers unit test as they program, they improve the longevity and quality of the codebase. The time a development team invests in unit tests pays off with less time spent troubleshooting defects and analyzing problems later.

Should unit tests cover all cases?

The purpose of a unit test in software engineering is to verify the behavior of a relatively small piece of software, independently from other parts. Unit tests are narrow in scope, and allow us to cover all cases, ensuring that every single part works correctly.

What is a reasonable code coverage for unit tests and why )?

While project wide goals above 90% are most likely not worth it, per-commit coverage goals of 99% are reasonable, and 90% is a good lower threshold. We need to ensure that our tests are not getting worse over time. Unit test code coverage is only a piece of the puzzle.

Is unit test coverage a good metric?

Criticism Of Code CoverageMany software testing experts argue that code coverage is not a good metric for software testing teams, even though it is often used to measure team performance. That's not to say coverage doesn't have its uses—as Martin Fowler points out, it is a good way to identify untested code.


2 Answers

I never write tests for these. There's no behavior to test.

If there were any non-trivial behavioral code whatsoever (validation code, perhaps), then I would write a test for it.

like image 83
Robert Harvey Avatar answered Oct 16 '22 00:10

Robert Harvey


I found this interesting article from Martin Fowler:

Test coverage is a useful tool for finding untested parts of a codebase. Test coverage is of little use as a numeric statement of how good your tests are.

===

Also this interesting quote from Object mentor:

It’s probably not mandatory and if your goal is 100% coverage, you’ll focus on that goal and not focus on writing the best tests for the behavior of your code.

like image 39
Alfred Avatar answered Oct 15 '22 22:10

Alfred