Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning signs for untestable code [closed]

Code that is untestable really annoys me. The following things make oo-code untestable:

  • global states, e.g., the Singleton Design Pattern
  • static methods that do some fancy work e.g database access
  • deep inheritance tree
  • work in constructor e.g. control statements
  • classes that violate the Single Responsibility Principle

Are there more warning signs?

like image 866
Ludwig Wensauer Avatar asked Jan 05 '09 07:01

Ludwig Wensauer


People also ask

What is untestable code?

(Part 2, for Developers) In my last post, I argued that “untestable code” is code that cannot be efficiently unit tested. Today I'd like to walk through my top five anti-patterns that make writing unit tests painful.

What is coding and unit testing?

In computer programming, unit testing is a software testing method by which individual units of source code—sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures—are tested to determine whether they are fit for use.

What do you call the practice of writing a test before the code is written?

TDD - Test Driven Development is a Development-Testing methodology that helps developers to achieve Speed, Robustness and Quality with its specifically structured mechanisms. It is a software development approach where a dev write unittests before the application code.


2 Answers

See the following blog post by Miško Hevery: How to Write 3v1L, Untestable Code.

like image 179
kshahar Avatar answered Oct 01 '22 18:10

kshahar


None of those things make code untestable. They may make it harder to find edge case bugs but, provided you have fully specified the success criteria for testing (and test-driven development eases this), all you have to do is pass the criteria.

TDD can apply to the behaviour of specific parts as well as the project as a whole, so you can easily test very small components. But, it's meant to test the results, not the means by which those results were obtained.

Provided the tests are passed, you have met the requirements. If there are bugs following that, this is an issue with the tests, not the code being tested (in which case the tests should be modified to catch the previously unforeseen problem).

You should not care (in terms of delivery of functionality) whether there's a while statement in one of your constructors. You should ask yourself what business requirement mandates that? I strongly doubt your client will deliver a list of requirements including "inheritance limited to 4 levels". They may well list "bug-free" as a requirement but you'll have to negotiate them down on that one :-).

like image 35
paxdiablo Avatar answered Oct 01 '22 18:10

paxdiablo