views:

2008

answers:

23

In which parts of a project writing unit tests is nearly or really impossible? Data access? ftp?

If there is an answer to this question then %100 coverage is a myth, isn't it?

+4  A: 

Data access is possible because you can set up a test database.

Generally the 'untestable' stuff is FTP, email and so forth. However, they are generally framework classes which you can rely on and therefore do not need to test if you hide them behind an abstraction.

Also, 100% code coverage is not enough on its own.

Garry Shutler
Tests based on a test database can still fail due to networking issues, unless you were referring to a in-memory database.
James McMahon
Glad to see someone advocating testing with real data. We generally mock/stub some data, but use our database to ensure tests are valid.
Jess
but talking to a test database would be an integration test and not a unit test... Wouldn't it?
Robert Koritnik
Technically, yes. But you would perform it with a unit testing framework so it depends how widely they are talking about unit tests.
Garry Shutler
@Garry: Yeah... Since we can use a "unit testing framework" to do both unit testing and integration testing, maybe it's time for us to start calling it simply "testing framework"
Alfred Myers
A: 

If the code to set up the state required for a unit test becomes significantly more complex than the code to be tested I tend to draw the line, and find another way to test the functionality. At that point you have to ask how do you know the unit test is right!

Rob Walker
+10  A: 

That 100% coverage is a myth, which it is, does not mean that 80% coverage is useless. The goal, of course, is 100%, and between unit tests and then integration tests, you can approach it.

What is impossible in unit testing is predicting all the totally strange things your customers will do to the product. Once you begin to discover these mind-boggling perversions of your code, make sure to roll tests for them back into the test suite.

Kevin Little
+1  A: 

Anything that needs a very large and complicated setup. Ofcourse you can test ftp (client), but then you need to setup a ftp server. For unit test you need a reproducible test setup. If you can not provide it, you can not test it.

EricSchaefer
+10  A: 

achieving 100% code coverage is almost always wasteful. There are many resources on this.

Nothing is impossible to unit test but there are always diminishing returns. It may not be worth it to unit test things that are painful to unit test.

aaronjensen
+1  A: 

@GarryShutler

I actually unittest email by using a fake smtp server (Wiser). Makes sure you application code is correct:

http://maas-frensch.com/peter/2007/08/29/unittesting-e-mail-sending-using-spring/

Something like that could probably be done for other servers. Otherwise you should be able to mock the API...

BTW: 100% coverage is only the beginning... just means that all code has actually bean executed once.... nothing about edge cases etc.

p3t0r
Yes, this is possible, but is it still unit testing, then ? Where do you draw the line between unit tests and integration tests ?
philippe
You are right; using fake servers would be integration testing... mocking out their interfaces would be unittesting.
p3t0r
A: 

FTP, email and so forth can you test with a server emulation. It is difficult but possible.

Not testable are some error handling. In every code there are error handling that can never occur. For example in Java there must be catch many exception because it is part of a interface. But the used instance will never throw it. Or the default case of a switch if for all possible cases a case block exist.

Of course some of the not needed error handling can be removed. But is there a coding error in the future then this is bad.

Horcrux7
+3  A: 

What would you not test? Anything that could not possibly break.

When it comes to code coverage you want to aim for 100% of the code you actually write - that is you need not test third-party library code, or operating system code since that code will have been delivered to you tested. Unless its not. In which case you might want to test it. Or if there are known bugs in which case you might want to test for the presence of the bugs, so that you get a notification of when they are fixed.

quamrana
"What would you not test? Anything that could not possibly break."Obviously, such things do not exist ;)
Thomas
I find that the stuff that cannot possibly break is where the hardest to find errors are. I wonder why?
Matthew Scouten
Steady on! I'm just quoting conventional Agile thinking: Test Everything That Could Possibly Break. Things like accessors are considered 'Unbreakable', so there is no need for an explicit test, they just get tested during the course of testing something else.
quamrana
+2  A: 

Most tests, that need huge and expensive (in cost of resource or computationtime) setups are integration tests. Unit tests should (in theory) only test small units of the code. Individual functions.

For example, if you are testing email-functionality, it makes sense, to create a mock-mailer. The purpose of that mock is to make sure, your code calls the mailer correctly. To see if your application actually sends mail is an integration test.

It is very useful to make a distinction between unit-tests and integration tests. Unit-tests should run very fast. It should be easily possible to run all your unit-tests before you check in your code.

However, if your test-suite consists of many integration tests (that set up and tear down databases and the like), your test-run can easily exceed half an hour. In that case it is very likely that a developer will not run all the unit-tests before she checks in.

So to answer your question: Do net unit-test things, that are better implemented as an integration test (and also don't test getter/setter - it is a waste of time ;-) ).

Mo
A: 

The main reason to unit test code in the first place is to validate the design of your code. It's possible to gain 100% code coverage, but not without using mock objects or some form of isolation or dependency injection.

Remember, unit tests aren't for users, they are for developers and build systems to use to validate a system prior to release. To that end, the unit tests should run very fast and have as little configuration and dependency friction as possible. Try to do as much as you can in memory, and avoid using network connections from the tests.

Kris
+1  A: 

You can test them, but they won't be unit tests. Unit test is something that doesn't cross the boundaries, such as crossing over the wire, hitting database, running/interacting with a third party, Touching an untested/legacy codebase etc.

Anything beyond this is integration testing.

The obvious answer of the question in the title is You shouldn't unit test the internals of your API, you shouldn't rely on someone else's behavior, you shouldn't test anything that you are not responsible for.

The rest should be enough for only to make you able to write your code inside it, not more, not less.

A: 

Sure 100% coverage is a good goal when working on a large project, but for most projects fixing one or two bugs before deployment isn't necessarily worth the time to create exhaustive unit tests.

Exhaustively testing things like forms submission, database access, FTP access, etc at a very detailed level is often just a waste of time; unless the software being written needs a very high level of reliability (99.999% stuff) unit testing too much can be overkill and a real time sink.

SoloBold
+2  A: 

Unit testing of a GUI is also difficult, albeit not impossible, I guess.

Thomas
There are some tools that do automated testing of GUIs, though I've not used any myself.
rlerallut
+2  A: 

In unit testing, you should not test anything that does not belong to your unit; testing units in their context is a different matter. That's the simple answer.

The basic rule I use is that you should unit test anything that touches the boundaries of your unit (usually class, or whatever else your unit might be), and mock the rest. There is no need to test the results that some database query returns, it suffices to test that your unit spits out the correct query.

This does not mean that you should not omit stuff that is just hard to test; even exception handling and concurrency issues can be tested pretty well using the right tools.

Angelo
+2  A: 

"What not to test when it comes to Unit Testing?" * Beans with just getters and setters. Reasoning: Usually a waste of time that could be better spent testing something else.

Vivek Kodira
+4  A: 

The goal is not 100% code coverage nor is it 80% code coverage. A unit test being easy to write doesn't mean you should write it, and a unit tests being hard to write doesn't mean you should avoid the effort.

The goal of any test is to detect user visible problems in the most afforable manner.

Is the total cost of authoring, maintaining, and diagnosing problems flagged by the test (including false positives) worth the problems that specific test catches?

If the problem the test catches is 'expensive' then you can afford to put effort into figuring out how to test it, and maintaining that test. If the problem the test catches is trivial then writing (and maintaining!) the test (even in the presence of code changes) better be trivial.

The core goal of a unit test is to protect devs from implementation errors. That alone should indicate that too much effort will be a waste. After a certain point there are better strategies for getting correct implementation. Also after a certain point the user visible problems are due to correctly implementing the wrong thing which can only be caught by user level or integration testing.

Steve Steiner
+26  A: 

Here I found (via haacked something Michael Feathers says that can be an answer:

He says,

A test is not a unit test if:

  • It talks to the database
  • It communicates across the network
  • It touches the file system
  • It can't run at the same time as any of your other unit tests
  • You have to do special things to your environment (such as editing config files) to run it.

Again in same article he adds:

Generally, unit tests are supposed to be small, they test a method or the interaction of a couple of methods. When you pull the database, sockets, or file system access into your unit tests, they are no’t really about those methods any more; they are about the integration of your code with that other software.

spinodal
+1  A: 

I disagree with quamrana's response regarding not testing third-party code. This is an ideal use of a unit test. What if bug(s) are introduced in a new release of a library? Ideally, when a new version third-party library is released, you run the unit tests that represent the expected behaviour of this library to verify that it still works as expected.

Mitch Wheat
Shouldn't integration tests catch this?Anyways, who are your third party vendors?You don't trust them to unit test their own code?Sure bugs can slip through, but if they missed a bug with their tests, your tests will probably miss the bug, too. (unless you are Godly or they just suck)What are you paying your vendors for if you have to regression test their code?
dss539
@dss539 : Third-party vendors are subject to the same development pressures as anybody else. If your line of business application depends on third party libraries, don't you think it might be prudent to migiate this risk by testing??
Mitch Wheat
+1  A: 

And also, some answers may be extracted from another questions (What makes a good unit test?) answers...

spinodal
+1  A: 

Configuration is another item that is very difficult to test well in unit tests. Integration tests and other testing should be done against configuration. This reduces redundancy of testing and frees up a lot of time. Trying to unit test configuration is often frivolous.

Adron
+2  A: 

Anything that is not completely deterministic is a no-no for unit testing. You want your unit tests to ALWAYS pass or fail with the same initial conditions - if weirdness like threading, or random data generation, or time/dates, or external services can affect this, then you shouldn't be covering it in your unit tests. Time/dates are a particularly nasty case. You can usually architect code to have a date to work with be injected (by code and tests) rather than relying on functionality at the current date and time.

That said though, unit tests shouldn't be the only level of testing in your application. Achieving 100% unit test coverage is often a waste of time, and quickly meets diminishing returns.

Far better is to have a set of higher level functional tests, and even integration tests to ensure that the system works correctly "once it's all joined up" - which the unit tests by definition do not test.

madlep
+2  A: 

Pex - Automated Whitebox Testing for .NET tries to achieve 100% coverage

YordanGeorgiev