473,772 Members | 2,244 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++ unit testing.

Hi, guys. I am interested in C++ unit testing. I know only two methods
how to test private class methods.

1) Declare friend class in the code. But I don't like this style
because tests depends on the code.
2) Cheat compiler. We can do it by:

#define private public
#include "..."

Okey-dockey, but we can't cheat linker. I'm using VC++ 7.0... and know
that methods with different access have different name in object
file... so, we can compile, but can't link.

Please share your experience, thanks in advance.

Jul 23 '05 #1
3 2701
Hm... I'm thinking and have some progress...
We can declare empty macros in code and the same macros in tests which
will declare class friends. i.e.:

class A
{
UNIT(A);

public:
....

private:
....
};

When compiling library this macros will be empty, but in tests we will
define:
#define UNIT(A) friend class Test##A

Then we will have friend class and can use it for testing purpose.

Any comments?

Jul 23 '05 #2
Hm... It works okay, but when Test class is in the same namespace as
tested class, otherwise it isn't work. How can I declare friend from
another namespace?

Jul 23 '05 #3
SnaiL wrote:
Hi, guys. I am interested in C++ unit testing. I know only two methods
how to test private class methods.

1) Declare friend class in the code. But I don't like this style
because tests depends on the code.
2) Cheat compiler. We can do it by:

#define private public
#include "..."

Okey-dockey, but we can't cheat linker. I'm using VC++ 7.0... and know
that methods with different access have different name in object
file... so, we can compile, but can't link.

Please share your experience, thanks in advance.

In general, unit testing private methods directly is not something that
you should aim to do. If the private method is worthy of additional
testing than it generally means your classes public methods are missing
certain tests.

However, there is another way to view the private method testing
dilemma. If a private method is worthy of testing to this extent, then
it can be an indication that the private method is doing 'too much'. In
other words, the private method is adding additional responsibilitie s to
its class. Aiming for classes which maintain the Single Responsibility
Principle aid your design in many ways. With this in mind, we can apply
the extractClass refactoring technique. Put simply, we create a new
class and move the private method into it, making the method public in
the process. Now we have just another normal class with a public
method, which can be tested without resorting to compiler/linker/code
tricks. Additional, once we have extracted the class, it can now be
reused by others.

There's been plenty of debate upon this subject, here's a google of
results for you to mull over.

http://www.google.co.uk/search?hl=en...e+Search&meta=

Andrew
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3740
by: Hugh Cowan | last post by:
Hello, I don't program full-time (anymore), but I do try and stay on-top of the latest technologies and like most are always trying to upgrade my skills and remain current (as much as is possible). Most of my programming these days involves using PHP for creating script files for automating tasks and procedures (locally), and also for anything that might be needed by our divisional Intranet (not a huge site by any stretch of the...
11
2333
by: rhat | last post by:
Hi Everyone, I've recently been reading some articles about unit-testing in Python , but I am a bit confused: where do I go to get started with this? I tried googling for "unittest" but all I've found are some old links to projects that already use it, and the older (as the articles put it) PyUnit project. I'm sorry if this is a obvious question or one that has already been answered, but unit-testing sounds interesting and I'm not sure...
14
2756
by: | last post by:
Hi! I'm looking for unit-testing tools for .NET. Somthing like Java has --> http://www.junit.org regards, gicio
4
2165
by: Peter Rilling | last post by:
Does VS.NET 2005 Professional support integrated unit testing, or is that only with the team system?
72
5279
by: Jacob | last post by:
I have compiled a set og unit testing recommendations based on my own experience on the concept. Feedback and suggestions for improvements are appreciated: http://geosoft.no/development/unittesting.html Thanks.
4
18969
by: Dat AU DUONG | last post by:
Hi, I am new to Unit testing, could you tell me where I could find information (hopefully step by step) and what is the benefit of unit testing. I am a sole developer in a company, therefore I don't get expose to much of this technology. Thanks in advance. Regards Dat.
5
6529
by: shuisheng | last post by:
Dear All, I was told that unit test is a powerful tool for progamming. If I am writing a GUI code, is it possible to still using unit test? I have a little experience in using unittest++. But I can not work out a way to use it to test GUI code. Thanks a lot!
176
8463
by: nw | last post by:
Hi, I previously asked for suggestions on teaching testing in C++. Based on some of the replies I received I decided that best way to proceed would be to teach the students how they might write their own unit test framework, and then in a lab session see if I can get them to write their own. To give them an example I've created the following UTF class (with a simple test program following). I would welcome and suggestions on how anybody...
48
2514
by: Ark Khasin | last post by:
Unit testing is an integral component of both "formal" and "agile" models of development. Alas, it involves a significant amount of tedious labor. There are test automation tools out there but from what limited exposure I've had, they are pricey, reasonably buggy, and require compiler/target adaptation. Out of my frustration with two out of two of them came my own. Its instrumentation approach is based solely on profound abuse of the C
5
2247
by: Ben Finney | last post by:
Howdy all, PEP 299 <URL:http://www.python.org/dev/peps/pep-0299details an enhancement for entry points to Python programs: a module attribute (named '__main__') that will be automatically called if the module is run as a program. The PEP has status "Rejected", citing backward-compatibility issues, and Guido's pronouncement that "It's not worth the change (in docs, user habits, etc.) and there's nothing particularly broken."
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10104
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9912
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6715
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.