Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do Test-Driven Development and the Open/Closed Principle work together?

I've been reading up on unit testing, TDD, and the SOLID principles and I need some clarification. My understanding is that if one adheres to the open/closed principle, unit testing could become largely unnecessary due to the fact that the code is closed to modification - thus there is no need to retest if the code is properly isolated and decoupled. The longterm benefit of the added upfront cost of unit testing is lost if once the code passes related unit tests it will not change. The code will always pass because it will never change, right? Inherited classes will need to be tested, but once they pass related tests, they too will be closed to modification and will not need to be retested. The Wikipedia article on OCP reinforces this line of thought in the first paragraph, (I realize that doesn't make it law).
The best explanation I've found for OCP and TDD living in harmony is here, though it seems that Arnon is saying that OCP compliments TDD in that the developer is discouraged from modifying source code because existing test methods can become complex when modified to test new functionality.
Is that all there is to it? Mind you that I'm not looking for an argument, I'm new to this and I'm looking for clarification from those with more experience with the subject than I.

like image 434
joelmdev Avatar asked Oct 26 '11 14:10

joelmdev


People also ask

How does Test-Driven Development relate to SOLID principles?

The SOLID principles, helps you to build a well designed application. From a TDD perspective, you can design your tests in a way where the complete behavior of the app is captured and tested, while minimizing the number of tests including redundant tests.

What is the principle of Test-Driven Development?

Test-driven development is the practice of developing software by first writing tests and then producing the minimum amount of code required to pass those tests. That statement pretty much captures it: you write clear requirements then you write enough code to meet them, and nothing more.

Why should the open-closed principle be used when designing a class?

The general idea of this principle is great. It tells you to write your code so that you will be able to add new functionality without changing the existing code. That prevents situations in which a change to one of your classes also requires you to adapt all depending classes.

What object-oriented concepts are needed to implement the open-closed principle?

In object-oriented programming, the open–closed principle states "software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification"; that is, such an entity can allow its behaviour to be extended without modifying its source code.


1 Answers

Even if you were to accept that once software works and doesn't change, you don't need to test it (which I don't), you still need to show that a component works in the first place. I would argue one of the best ways to do that is a unit test(s).

Also, practically speaking, once you have the test, it costs very little to run it. So even if components don't change, you aren't losing much by continually running the tests. Furthermore, sometimes design changes and you need to go back and redo some components -- having unit tests definitely helps in this case...

Remember, one result of having a test suite is that it promotes maintainability by showing when something changes. So even if your team is following the O in SOLID, that doesn't mean that things won't get changed accidentally. So the tests help there by showing if something is changed inadvertently, and they show you exactly what was affected by the change.

like image 194
hvgotcodes Avatar answered Sep 29 '22 06:09

hvgotcodes