473,398 Members | 2,165 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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 2674
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 responsibilities 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
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...
11
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...
14
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
by: Peter Rilling | last post by:
Does VS.NET 2005 Professional support integrated unit testing, or is that only with the team system?
72
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: ...
4
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...
5
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...
176
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...
48
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...
5
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.