[Idea] unit tests

64 views
Skip to first unread message

kocz...@gmail.com

unread,
Oct 14, 2017, 10:24:05 AM10/14/17
to ISO C++ Standard - Future Proposals
Currently C++ requires additional frameworks to provide any decent unit tests and it requires a lot of macros. Some other languages, like D, provide built-in unit tests as a part of the language and it makes code a lot cleaner. I personally really like UT implementation in D, you can take a look at it here.
Do you think C++ would benefit from such feature? Why (not)?

Magnus Fromreide

unread,
Oct 14, 2017, 11:32:06 AM10/14/17
to std-pr...@isocpp.org
On Sat, Oct 14, 2017 at 07:24:05AM -0700, kocz...@gmail.com wrote:
> Currently C++ requires additional frameworks to provide any decent unit
> tests and it requires a lot of macros. Some other languages, like D,
> provide built-in unit tests as a part of the language and it makes code a
> lot cleaner. I personally really like UT implementation in D, you can take
> a look at it here <https://dlang.org/spec/unittest.html>.
> Do you think C++ would benefit from such feature? Why (not)?

Do you have anything different than

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4420.pdf

in mind?

/MF

John McFarlane

unread,
Oct 14, 2017, 11:36:39 AM10/14/17
to std-pr...@isocpp.org
D's solution goes further than the proposal, addressing some of the features listed under "Not Included in this Proposal", most notably a means for running the tests.  It also doesn't require that each test be in a function with name and return type etc..

/MF

--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposal...@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/20171014153154.GA1546%40fukushima.lysator.liu.se.

John McFarlane

unread,
Oct 14, 2017, 11:49:48 AM10/14/17
to std-pr...@isocpp.org
On Sat, Oct 14, 2017 at 7:24 AM <kocz...@gmail.com> wrote:
Currently C++ requires additional frameworks to provide any decent unit tests and it requires a lot of macros. Some other languages, like D, provide built-in unit tests as a part of the language and it makes code a lot cleaner. I personally really like UT implementation in D, you can take a look at it here.
Do you think C++ would benefit from such feature? Why (not)?

It might but perhaps not that much. 

Firstly, whenever a library can provide the same functionality as a language feature, the former tends to be favored.  D's `unittest` -- while clean and convenient are far necessary.

Regarding macros, C++ is gradually seeking out superior solutions in cases where macros are used currently.  A good example here is finding an alternative to macros that use `__LINE__` and `__FILE__`[1].  Generally, I would expect many of the disadvantages of C++ test frameworks to be addressed over time with general solutions that extend far beyond this one use case.

Some people are very used to separating tests from the code that is being tested -- often into an entirely different project root directory.  This can be an obstacle to acceptance of a feature which aims to improve locality -- as D tests tend to.

Then there are the alternatives.  `static_assert` does not require additional frameworks to function and does not even incur a runtime cost.  For the diminishing number of tests that cannot be performed at compile time, `assert` and contracts[2] -- while not solely intended for the purpose of unit testing -- play a significant role in testing code and ensuring correctness.

So overall, I would not be too hopeful for such a proposal per se but I'm in favor of improvements that head in the same general direction.

Thanks,
John

1. wg21.link/N4519
2. wg21.link/P0542R1
Reply all
Reply to author
Forward
0 new messages