April 22, 2016 Re: Enabling Only Top-Level Unittests | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | On Thursday, 21 April 2016 at 19:38:21 UTC, Nordlöw wrote: > On Thursday, 21 April 2016 at 12:35:42 UTC, Anonymouse wrote: >> Then dmd -unittest -version=TestDeps if you want them run. > > This doesn't make things easier. I want to disable the builtin unittests of the modules I've imported. This requires me to add a > > version(test_MODULE) unittest > > in each module named MODULE. > > This is really inconvenient. You could anotate your user/top level tests and select only those with __traits(getUnittest,...) and hasUDA(). Unfortunately it seems that there's a flaw because in a program all the tests will be automatically executed once then again those that are anotated. --- #!runnable-flags: -unittest module m; import std.stdio; enum USERTEST; @USERTEST unittest // exeuted twice { writeln("bang"); } unittest // executed once { writeln("bing"); } void main(string[] args) { import std.traits; foreach(t; __traits(getUnitTests, m)) { static if (hasUDA!(t, USERTEST)) t(); } } --- But... A solution that would work for both a library and a program is this: --- module m; import std.stdio; enum USERTEST; @USERTEST void test0() { writeln("bing"); } @USERTEST void test1() { writeln("bang"); } unittest { // not even compiled } void runUserTests(Modules...)() { import std.traits; static if (Modules.length > 1) runModuleTests!(Modules[1..$]); else static if (Modules.length == 1) { foreach(member; __traits(allMembers, Modules[0])) foreach(o; __traits(getOverloads, Modules[0], member)) static if (hasUDA!(o, USERTEST) && (Parameters!o).length == 0) o(); } } void main() { runUserTests!m; } --- I think a DMD option should be added to allow the tests to be compiled but never called, something like -runTests. Because the first solution is much more attractive. |
April 23, 2016 Re: Enabling Only Top-Level Unittests | ||||
---|---|---|---|---|
| ||||
Posted in reply to Basile B. | On Friday, 22 April 2016 at 18:18:39 UTC, Basile B. wrote: > I think a DMD option should be added to allow the tests to be compiled but never called, something like -runTests. Because the first solution is much more attractive. Actually the first solution works with: https://dlang.org/phobos/core_runtime.html#.Runtime.moduleUnitTester --- #!runnable-flags: -unittest -main module m; import std.stdio; import core.runtime; enum USERTEST; @USERTEST unittest // exeuted once { writeln("bang"); } unittest { writeln("bing"); } bool tester() { import std.traits; foreach(t; __traits(getUnitTests, m)) { static if (hasUDA!(t, USERTEST)) t(); } return true; } static this() { Runtime.moduleUnitTester = &tester; } --- So there's really no problem with selecting the top-level UT. Maybe it even works with other traits such as "parent". Or with a template that collects only the tests in a set of module like in the previous attempt (based on this: http://stackoverflow.com/questions/35075253/is-it-possible-to-compile-unittest-without-running-them-and-explicitly-run-unitt/35086752#35086752). |
Copyright © 1999-2021 by the D Language Foundation