I read many comparisons among unit testing frameworks, but sincerely I
can't decide which one to use.
Do you use any? Which one and why?
Am 26.01.2022 um 13:23 schrieb pozz:
I read many comparisons among unit testing frameworks, but sincerely I
can't decide which one to use.
Do you use any? Which one and why?
None. Just <assert.h>.
For me, the main objective for a unit test framework is to have some syntactic sugar for assertions and, less important, for grouping tests
into test suites and stuff.
Do we need assertions? It is surely helpful, if an equality assertion
fails, to have expected and actual value presented on a golden plate. On
the other hand, getting those values with a debugger isn't much more
complex, and you typically fire up a debugger anyway to investigate. In
a language such as Java or C++, you can have some higher-level
assertions such as "assert-throws" or "assert-container-contains", which
save quite a lot of typing. In C, the highest level you could get is something like "assert-string-equals", but the gain compared to plain 'assert(strcmp(...) == 0)' is vanishingly small.
Do we need to group tests? People delight in numbers, "I have 937 unit
tests, and of those, 936 pass, look how awesome I am!". But, seriously,
even if one test fails, the product is broken and someone needs to investigate, so, for me, unit tests produce a "ok"/"nok" result, so I
just need a binary that runs or crashes.
Now, what I don't expect in a unit test framework proper is the whole
area of mocking stuff. In languages like Java, how to mock something is pretty straightforward: you use abstract interfaces, factories, etc.,
and to mock a file system access, you pass a mock FileSystem object. In
C, how you can mock - if you mock at all - depends on your codebase. Do
you have the equivalent of virtual interfaces? Do you want to play
linker tricks? Do you want to play preprocessor tricks? That decides
what mocking framework, if any, you can use.
Personally, when I unit-test C, I'm testing computations and logic,
which usually don't need mocking. Things like: put in this input, expect
this output, from my computational kernel.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 495 |
Nodes: | 16 (3 / 13) |
Uptime: | 42:10:51 |
Calls: | 9,745 |
Calls today: | 5 |
Files: | 13,742 |
Messages: | 6,183,865 |