Testing a device using -fno-access-control

I saw a lot of crazy ways to access private variables in unit testing. The most amazing thing I've seen is #define private public .

However, I have never seen anyone suggest disabling private variables at the compiler level. I always only assumed that you could not. I complained to many developers that unit testing would be much easier if you just tell the compiler to back down for this file.

Then I came across the -fno-access-control GCC compiler option. This is so obviously the perfect way for unit testing. Your source files are not changed, friends are not added only for the unit test, there is no recompilation with fancy preprocessor magic. Just click the "no access control" switch when compiling unit tests.

Did I miss something? Hope this is a test silver bullet?

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

+6
c ++ gcc unit-testing
source share
4 answers

I would say that unit tests do not need access to private members.

In general, unit tests are designed to test the interface for your classes, and not for internal implementation. Thus, changes to internal components will only violate tests if the interface has been compromised.

Look at my answer to a similar question and the following discussion. Of course, this is a controversial topic, but my $ 0.02.

+6
source share

Usually I try to use only the public interface of my classes in unit tests. Test-based development / design helps a lot here, since the resulting classes tend to include this unit test style.

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

Some people seem to lean back to avoid friends, but they should be used when necessary, and their use does not compromise the design. They are also declarative and let other programmers know what you are doing.

+2
source share

Wow, this worked fine for me.

I was worried that my unittests should gain access to private class members embedded in other dynamic libraries (.so files), but this is exactly what I need.

I need to declare a flag only in my unit test.so compilers (each test is.so). Even in libraries where accessible objects are defined.

I needed to access the internal widgets in the form in order to fill in their values; they are not visible to the rest of the program, but are necessary if my tests introduce a user entering input. Just thought that I would share a precedent for those who have access to private access :)

Also for completeness, here is my form class showing the private name_ field:

 struct EditProduct : public widgets::BusinessObjForm<model::Product> { public: EditProduct (WContainerWidget *parent=0); protected: void fillObjFields(); private: // Consts static const double minPrice = 0.0; static const double maxPrice = 10000.0; // Fields WLineEdit* name_; WTextEdit* description_; WSpinBox* price_; WFileUpload* image_; // Methods bool validate(); void saveProduct(const WString& message); }; 

and here is the beginning of my unit test access to this widget:

 BOOST_AUTO_TEST_CASE( form_save_test ) { EditProduct form(app.root()); string txt = "this is a product"; form.name_->setText(txt); BOOST_CHECK_EQUAL(form.name_->text(), txt); } 
+1
source share

Yeap is rather a useful version of GCC, but MSVC has nothing of the kind.
Instead, we use macros:

 #define class struct #define private public #define protected public 

^ _ ^

-2
source share

All Articles