Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing with -fno-access-control

I have seen many crazy methods to get access to private variables when unit testing. The most mind-blowing I've seen is #define private public.

However, I've never seen anyone suggest turning off private variables at the compiler level. I had always just assumed that you couldn't. I've complained to many a developer that unit testing would be a lot easier if you could just tell the compiler to back off for this one file.

Then I stumble across the -fno-access-control GCC compiler option. It's so obviously the perfect way to unit test. Your original source files are unmodified, no injected friends just for the unit test, no recompiling with bizarre preprocessor magic. Just flick the 'no access control' switch when compiling your unit tests.

Am I missing something? Is this the unit testing silver bullet I hope it is?

The only disadvantage I see is the GCC-specific nature of the technique. However, I assume MSVS has a similar flag.

like image 295
deft_code Avatar asked May 13 '09 23:05

deft_code


People also ask

What is unit testing with example?

What is Unit Testing? Unit testing is testing the smallest testable unit of an application. It is done during the coding phase by the developers. To perform unit testing, a developer writes a piece of code (unit tests) to verify the code to be tested (unit) is correct.

What is used for unit testing?

Modern versions of unit testing can be found in frameworks like JUnit, or testing tools like TestComplete. Look a little further and you will find SUnit, the mother of all unit testing frameworks created by Kent Beck, and a reference in chapter 5 of The Art of Software Testing .

What is meant by unit testing?

What is Unit Testing. Definition: This is a type of testing which is done by software developers in which the smallest testable module of an application - like functions, procedures or interfaces - are tested to ascertain if they are fit to use.


2 Answers

I normally try to use only the public interface of my classes in unit tests. Test Driven Development/Design helps a lot here as the resulting classes tend to enable this style of unit test.

However sometimes you do need to let a unit test access non public members, eg replace the contents of a Singleton with a Fake instance. For this I use package protection in Java and friends in C++.

Some people seem to bend over backwards to avoid friends but they should be used when appropriate and their use doesn't compromise the design. They are also declarative and let other programmers know what you are doing.

like image 42
iain Avatar answered Sep 19 '22 12:09

iain


I would argue that unit tests should not need access to private members.

In general, unit tests are meant to test the interface to your classes, not the internal implementation. That way, changes to the internals will only break the tests if the interface has been compromised.

Have a look at my answer to a similar question, and the ensuing discussion. It is a controversial topic, to be sure, but that's my $0.02.

like image 132
e.James Avatar answered Sep 21 '22 12:09

e.James