`unittest` placement in code?

Anonymouse zorael at gmail.com
Mon Oct 26 15:57:56 UTC 2020


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.


More information about the Digitalmars-d-learn mailing list