How to include my testable project in my Unit test project - c++

I'm trying to create a unit test in QTCreator so I've followed this tutorial from the QT Docs:
https://doc.qt.io/qt-6/qttestlib-tutorial1-example.html
This runs example fine for me.
Unfortunately, this tutorial, and a few others I've found all give examples referencing classes included within the Unit Test project itself. But I would have thought the proper way would be to use my Unit Test project to create tests for the separate actual application that I'm trying to test. (after all, I can't add QTEST_MAIN to my own project as it has its own main).
So how do I include the testable in the unit test project and refer to the functions that I am trying to test?

Related

Should I create a separate project to do Unit Test? I'm using JUnit

I'm new to software testing. I want to use JUnit to do the unit test on a Java project. The project is open source and it's a small application. So my question is - should I create a separate project to do testing or write the test code within the project? I use Netbeans btw. Also, can I generate test cases from existing class? On Netbeans, when you click File->New File->Unit Tests(Choose File Type)->Test for existing class, you are probably supposed to generate a test file but it turns out that you will just create new empty file... at least on my laptop... I'm not sure if that's the problem of the IDE..
Anyway, any answers would be appreciated. Thanks.
I suggest you use Maven's default project structure:
<projectDir>
<src>
<main>
<java>
<packages>
<test>
<java>
<packages>
In this structure your tests are in the same project as your code but to not get compiled into your jar. Generally you put a unit test in the same package (under src/test/java) as the class under test. This gives organization and allows the unit test to access package-private (default scope) methods.

Using QT Unit Tests in a project - conflicting main(...) functions

I set my first steps upon the noble path of using unit tests to develop my application, but it proves to be a steep and rough one. I'm developing an application in Qt, so thought to reach for their QTestLib framework. Now, I understand how to make unit tests, but I can't seem to figure out how to incorporate unit testing into a project.
Say I have a console application which just has a main.cpp and a cpp and h file for a class I want to develop, say MyClass. I guess the right thing to do would be to create a corresponding test class MyClassTest with its h and cpp files. But should I put it into the same project? Or rather create a separate project just for unit tests that will have access to files in "main" project (not sure how would I do that yet)?
And if both main application and the test are in the same project, how do I run tests without running the application or the other way round? I tried incorporating files from Qt's Tutorial 1 on unit testing into a console project, but the problem with it is that they use a macro QTEST_MAIN(TestQString) which expands into a main(...) function. This causes conflicts with the main(...) in the main.cpp. Well, I can rename either one, but the problem still stands - how do I then run either the tests or the application itself? If I override QTEST_MAIN macro (which is a bad idea anyway) I disable the tests. If I rename main(...) function in the main.cpp, then the actual application never gets executed. I guess there should be a way to make two separate executables for the project, one running unit tests, and another launching the application, but can't figure out how to go about it.
Definitely create a separate project for your tests. If you arrange the application project and test project in side-by-side directories, you should be able to reference your code units from your application project using relative paths in your test.pro file.
I use the very handy test runner presented on this blog.

Unit tests in a separate project

Long time viewer, first time questioner here. Using Unit Tests in a separate project, I can't figure out how to separate them at the release build?
Surely when I remove the reference to the Unit Test project, all the references to the Unit Test interfaces, won't be able to find the interfaces and will cause compiler errors?
Thanks for any help.
Edit: By separating I mean removing them on the release build.
Assuming .NET, your unit test project should have a reference to the production project - but not the other way round. Your production code shouldn't depend on your unit tests at all.
You should have built your two projects, so that the base code (That in which you are testing) has no dependencies on the unit test. It is the Unit Test that has the dependencies on your code.
From what you wrote, I gather you wrote your code in .NET? (i.e. Assemblies?). Your project dependencies should look like this.
// Business code
My_Project.dll
-> References
---> System.dll etc..
// Test code
My_Project_Test.dll
-> References
---> My_Project.dll
If you make your projects that way, than I think you won't have to do anything special in your release build configuration.

Organizing unit testing for existing code

I recently received as a new task to maintain and improve existing code written in C++ with MS Visual Studio. The code builds into an exe file (not a dll). I would like to add unit tests for the code and the problem I encountered is how to organize my testing projects. Basically I want to have 2 projects, one would be the original project I received and the second the testing project.
I saw on the Internet that usually when the subject being tested is built into a dll it's quite easy you have to statically link in your testing project the lib built from the main project and you have access to the function being tested. But how can this be done when the subject under test is an exe file?
Surely you can arrange the solution into projects that share code, where one project outputs to exe and the other(s) to DLL?
Whatever the project deliverable is, unit testing is testing the smallest units: the functions. A unit test typically follows the tripe A pattern: Arrange (create the environment for the test), Act (invoke the method under test), Assert (verify the method behaved as expected).
There are several possible project[s] structures: modify the project so that it compiles into a DLL, a production executable and a unit test program. The executable source has to be as small as possible, possibly just the main() function that create an Application object. It is also possible to have three projects, one for the DLL, one for the application and the third one for the tests.
An alternative is to embed the unit tests inside the executable and to have a mean to invoke them, e.g. with a special --unit-test parameter.

Seeking suggestions for Unit Testing C++ app spread over several dlls

New to unit testing and have an app that is spread out over several dlls. What are some suggestions for unit testing the app? If I put the unit tests in their own project, i can only test the published interface for each dll. Is that what most people do? Or should I put the unit tests in with the code in the dlls and test there? What would be the best way to coordinate all the tests at that point?
Most of the examples I've seen for using the various frameworks don't really address this and I've search elsewhere but haven't found much information.
Thanks.
Update:
I guess to clarify a bit I have class A and class B in a dll. Class B is used by Class A, but only Class A is exposed. I'm wanting to get unit tests in place before refactoring this existing code.
So the question is should I put unit tests in with the dll code to test class A and class B directly and/or have the unit tests in a separate project and test class A and class B through the exposed class A?
My home-rolled approach to this is to build two different targets:
the DLL itself
a test executable
The code base is the same except for main.cpp which will contain a DLL main for the DLL and a standard C/C++ int main() for the test executable. All the unit tests are in-line with the actual code, controlled by a preprocessor #define:
#ifdef TESTING
tests here
#endif
The main() for the executable calls all the tests via a registration framework.
Most of the time I work with the executable version with TESTING defined. When I want to build the DLL, I switch targets and undefine TESTING.
Unit testing in C++ means rather class testing. DLL testing would be rather integration testing. Both are necessary, but it's better to test things at as low level as possible. Take a look on v-model: http://en.wikipedia.org/wiki/V-Model_(software_development).
If I put the unit tests in their own project, i can only test the published interface for each dll.
As opposed to features in the DLL that client code cannot access?
If code doesn't contribute to the functions the DLL exposes, then delete it from the code base. If it does, then you can write a test that exercises that code path.
In general, thoroughly testing the public interface of a DLL should be sufficient and, as Pete indicated, should indeed exercise all code paths.
Regarding Neils' answer, choosing a test framework that does not require you to build EXEs but rather directly operates on DLLs could further simplify matters. Being the author of cfix, I'd naturally recommend giving cfix a try: http://www.cfix-testing.org/
I use boost.test to test my dlls and executables. I create a seperate unit test project/executable for each dll and exe to test. Because I test the internals of the dll I do not link them with the test project, but include the source I want to test directly in the project. Finally the automated build runs all the unit test projects.
We use CMake to build our projects and this has a nice component CTest that bundles up all our tests for us and runs them as a group.