www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - `unittest` placement in code?

reply Vladimirs Nordholm <v vladde.net> writes:
Hello.

I have a class which I have written some tests for, to ensure if 
I ever change some code it will still work as intended.

The documentation https://dlang.org/spec/unittest.html says it is 
can be placed both in the class or outside it.

I come from a background of having a completely separate tests 
folder with only tests, so I do not know what the general 
best-practice is with D. Where should the `unittest` code block 
go?
Oct 26 2020
next sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 10/26/20 9:16 AM, Vladimirs Nordholm wrote:
 Hello.
 
 I have a class which I have written some tests for, to ensure if I ever 
 change some code it will still work as intended.
 
 The documentation https://dlang.org/spec/unittest.html says it is can be 
 placed both in the class or outside it.
 
 I come from a background of having a completely separate tests folder 
 with only tests, so I do not know what the general best-practice is with 
 D. Where should the `unittest` code block go?
Wherever you want. Generally people put it right after the thing being tested to keep files organized. When the compiler runs unittests it runs them all in the sequence they are in the file. One place to be cautious though -- if you put them inside a template, they will be created for every instantiation of that template. -Steve
Oct 26 2020
parent Vladimirs Nordholm <v vladde.net> writes:
On Monday, 26 October 2020 at 13:36:58 UTC, Steven Schveighoffer 
wrote:
 On 10/26/20 9:16 AM, Vladimirs Nordholm wrote:
 [...]
Wherever you want. Generally people put it right after the thing being tested to keep files organized. When the compiler runs unittests it runs them all in the sequence they are in the file. One place to be cautious though -- if you put them inside a template, they will be created for every instantiation of that template. -Steve
Thanks for the explanation (and the warning) Steve :)
Oct 26 2020
prev sibling parent Anonymouse <zorael gmail.com> writes:
On Monday, 26 October 2020 at 13:16:32 UTC, Vladimirs Nordholm 
wrote:
 Hello.
 [...]
Additionally, if you care about generating documentation, then placing them after whatever is being tested can make them end up as examples (of how to use them). Handy if your unittests shows how they're used and not only tests that they work; not so much otherwise. class Foo { /// asdf int multiplier; /++ Multiplies stuff by `multiplier`. Params: n = Number to multiply. Returns: The integer passed, multiplied by `multiplier`. +/ int mul(int n) { return multiplier * n; } /// unittest { // Will end up in examples section, provided /// is placed above unittest Foo foo = new Foo; foo.multiplier = 2; int four = foo.mul(2); int six = foo.mul(3); int eight = foo.mul(4); int ten = foo.mul(5); } } https://i.imgur.com/zjuJQkR.jpg Even so I keep my larger test suite in a separate tests/ directory next to source/, but mostly because they'd swamp the original source files. You also lose the ability to unittest private stuff this way.
Oct 26 2020