Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Too Many Public Methods Forced by Test Driven Development

Tags:

tdd

A very specific question from a novice to TDD:

I separate my tests and my application into different packages. Thus, most of my application methods have to be public for tests to access them. As I progress, it becomes obvious that some methods could become private, but if I make that change, the tests that access them won't work. Am I missing a step, or doing something wrong, or is this just one downfall of TDD?

like image 363
RoryG Avatar asked Mar 17 '10 19:03

RoryG


People also ask

What are the disadvantages of Test-Driven Development?

Cons of Test Driven Development In the beginning, it may slow down development, but in the long run, it actually speeds up development. The whole team needs to buy into Unit testing for it to work well. Difficult to apply to existing and/or legacy code.

Why TDD is not usually used?

This means the following problems in such a TDD approach: More test code than the implementation code. Not easy to design tests before the implementation is done. Implementation refactoring breaks existing tests.

Do people actually use Test-Driven Development?

Test driven development has become popular over the last few years. Many programmers have tried this technique, failed, and concluded that TDD is not worth the effort it requires. Some programmers think that, in theory, it is a good practice, but that there is never enough time to really use TDD.

What are the major challenges faced when performing Test-Driven Development?

Writing GUI code with TDD is difficult. One area of TDD that's particularly challenging is programming GUIs. User interfaces are complex and difficult to test. They may also rely heavily on libraries, which can make writing clean tests and working through a design much more limited.


1 Answers

This is not a downfall of TDD, but rather an approach to testing that believes you need to test every property and every method. In fact you should not care about private methods when testing because they should only exist to facilitate some public portion of the API.

Never change something from private to public for testing purposes!

You should be trying to verify only publicly visible behavior. The rest are implementation details and you specifically want to avoid testing those. TDD is meant to give you a set of tests that will allow you to easily change the implementation details without breaking the tests (changing behavior).

Let’s say I have a type: MyClass and I want to test the DoStuff method. All I care about is that the DoStuff method does something meaningful and returns the expected results. It may call a hundred private methods to get to that point, but I don't care as the consumer of that method.

like image 96
Josh Avatar answered Sep 20 '22 15:09

Josh