Unit Testing / TDD

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
adrianinsaval
Veteran
Posts: 5541
Joined: Thu Apr 05, 2018 5:15 pm

Re: Unit Testing / TDD

Post by adrianinsaval »

Thanks! I'll be looking into these
FreddyFreddy
Posts: 176
Joined: Wed Mar 09, 2022 3:15 am
Location: Oz

Re: Unit Testing / TDD

Post by FreddyFreddy »

wmayer wrote: Tue Jun 14, 2022 10:22 am Here is a video how to do it with QtCreator: https://www.youtube.com/watch?v=N4pvvCToogM
and documentation: https://doc.qt.io/qtcreator/creator-autotest.html
I've loaded QtCtreator and got it going, but it really isn't as nice as CLion. Feels clunky. Having said that there are some features of QtCtreator that may be better adapted to test some qt based stuff.

The bigger question though is it there a way to begin transforming FreeCAD codebase to modern style. The cat and mouse question is that to safely refactor you need tests, but to get to having tests you have to refactor.

How does this discussion get started? Is there a will?
User avatar
doia
Posts: 251
Joined: Sat May 29, 2021 5:47 am
Location: Düsseldorf

Re: Unit Testing / TDD

Post by doia »

Oh yes, there is a will.

For unit tests I would suggest following some advice from the "Canonical Project Structure" initiative, see the part about tests: https://www.open-std.org/jtc1/sc22/wg21 ... html#tests

- Unit tests live as a source.test.cpp file next to the source.h and source.cpp files
- Functional and integration tests go into a separate tests/ folder
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Unit Testing / TDD

Post by wmayer »

I've loaded QtCtreator and got it going, but it really isn't as nice as CLion. Feels clunky. Having said that there are some features of QtCtreator that may be better adapted to test some qt based stuff.
Nobody is obliged to use QtCreator and I added links because I use this IDE on Linux. In the video and the tutorial it's explained how to set up the infrastructure for the testing framework and ideally it should work independent of the used IDE because it's all based on CMake.
FreddyFreddy
Posts: 176
Joined: Wed Mar 09, 2022 3:15 am
Location: Oz

Re: Unit Testing / TDD

Post by FreddyFreddy »

wmayer wrote: Thu Jun 23, 2022 2:28 pm
it should work independent of the used IDE because it's all based on CMake.
Could you expand on that a bit?
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Unit Testing / TDD

Post by wmayer »

Could you expand on that a bit?
Not yet. I have to figure out first how to set it up.
FreddyFreddy
Posts: 176
Joined: Wed Mar 09, 2022 3:15 am
Location: Oz

Re: Unit Testing / TDD

Post by FreddyFreddy »

I guess Cmake will be involved in setting up the test environment as tests must be compiled.

The greater difficulty I see is to have code under test that is a unique unit of behaviour (eg class etc), dependancies well defined, pref injected via constructor (inverted) via interfaces so that service objects can be easily mocked. Macros, inheritance, dependencies, wandering state, multiple responsibilities, contrive to make unit testing extremely difficult or impossible. Individual tests must be very focused and execute very fast, as there will be loads of them and they will be run frequently during refactoring to identify problems early.

So far I am overwhelmed by how much change (refactoring) might be required to the codebase just to make a start. The python interface frightens me too.
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Unit Testing / TDD

Post by wmayer »

Here is the initial setup of Google's testing framework: https://github.com/FreeCAD/FreeCAD/pull/7903
User avatar
chennes
Veteran
Posts: 3877
Joined: Fri Dec 23, 2016 3:38 pm
Location: Norman, OK, USA
Contact:

Re: Unit Testing / TDD

Post by chennes »

What is the planned structure of where to put the test code? For example, if I want to get src/App/Metadata.cpp under test, should I make src/App/Metadata.test.cpp, or should I make ./tests/src/App/Metadata.cpp, or ./tests/src/App/Metadata.test.cpp, or something else entirely? (For C++ I normally use Catch2 so it will take me some time to get up to speed on GoogleTest -- the documentation looks solid, anyway).
Chris Hennes
Pioneer Library System
GitHub profile, LinkedIn profile, chrishennes.com
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Unit Testing / TDD

Post by wmayer »

What is the planned structure of where to put the test code? For example, if I want to get src/App/Metadata.cpp under test, should I make src/App/Metadata.test.cpp, or should I make ./tests/src/App/Metadata.cpp, or ./tests/src/App/Metadata.test.cpp, or something else entirely?
I recommend to create a parallel structure under ./tests/src, i.e. ./tests/src/App/Metadata.cpp in your case. Putting it into the directory of the production code doesn't seem to be a good idea, IMO.
(For C++ I normally use Catch2 so it will take me some time to get up to speed on GoogleTest -- the documentation looks solid, anyway).
According to the Qt documentation Google Test, QtUnit, boost.test and Catch2 are supported by QtCreator. However, my installation doesn't seem to support the latter two.

And about Visual Studio I don't know which frameworks it supports. Today I have tested the two frameworks (Google Test and QtUnit) and it only found the Google tests.
Post Reply