Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Pros</strong></p> <ul> <li>You can access the private members to test them</li> <li>Its a fairly minimal amount of <code>hack</code></li> </ul> <p><strong>Cons</strong></p> <ul> <li>Broken encapsulation</li> <li>Broken encapsulation that is more complicated and just as brittle as <code>friend</code></li> <li>Mixing test with production code by putting <code>test_backdoor</code> on the production side</li> <li>Maintance problem ( just like friending the the test code, you've created an <em>extremely</em> tight coupling with your test code ) </li> </ul> <hr> <p>All of the Pros/Cons aside, I think you are best off making some architectural changes that allow better testing of whatever complex stuff is happening.</p> <p><strong>Possible Solutions</strong></p> <ul> <li>Use the Pimpl idiom, put the <code>complex</code> code in the pimpl along with the private member, and write a test for the Pimpl. The Pimpl can be forward declared as a public member, allowing external instantiation in the unit test. The Pimpl can consist of only public members, making it easier to test <ul> <li>Disadvantage: Lots of code</li> <li>Disadvantage: opaque type that can be more difficult to see inside of when debugging</li> </ul></li> <li>Just test the public/protected interface of the class. Test the contract that your interface lays out. <ul> <li>Disadvantage: unit tests are difficult/impossible to write in an isolated manner.</li> </ul></li> <li>Similar to the Pimpl solutions, but create a free function with the <code>complex</code> code in it. Put the declaration in a private header ( not part of the libraries public interface ), and test it.</li> <li>Break encapsulation via friend a test method/fixture <ul> <li>Possible variation on this: declare <code>friend struct test_context;</code>, put your test code inside of methods in the implementation of <code>struct test_context</code>. This way you don't have to friend each test case, method, or fixture. This should reduce the likelyhood of someone breaking the friending.</li> </ul></li> <li>Break encapsulation via template specialization </li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload