Unit Testing / TDD
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
- adrianinsaval
- Veteran
- Posts: 5551
- Joined: Thu Apr 05, 2018 5:15 pm
Re: Unit Testing / TDD
Thanks! I'll be looking into these
-
- Posts: 176
- Joined: Wed Mar 09, 2022 3:15 am
- Location: Oz
Re: Unit Testing / TDD
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.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
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?
Re: Unit Testing / TDD
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
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
Re: Unit Testing / TDD
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.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.
-
- Posts: 176
- Joined: Wed Mar 09, 2022 3:15 am
- Location: Oz
Re: Unit Testing / TDD
Not yet. I have to figure out first how to set it up.Could you expand on that a bit?
-
- Posts: 176
- Joined: Wed Mar 09, 2022 3:15 am
- Location: Oz
Re: Unit Testing / TDD
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.
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.
Re: Unit Testing / TDD
Here is the initial setup of Google's testing framework: https://github.com/FreeCAD/FreeCAD/pull/7903
Re: Unit Testing / TDD
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).
Re: Unit Testing / TDD
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.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?
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.(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).
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.