Unit tests via DUB

Alexander Zhirov alexander at zhirov.website
Sat Apr 2 16:05:01 UTC 2022


On Saturday, 2 April 2022 at 14:35:49 UTC, Salih Dincer wrote:
> On Saturday, 2 April 2022 at 13:31:47 UTC, Alexander Zhirov 
> wrote:
>> On Saturday, 2 April 2022 at 13:12:04 UTC, Salih Dincer wrote:
>>> On Saturday, 2 April 2022 at 11:53:12 UTC, alexanderzhirov 
>>> wrote:
>>>> I don't quite understand why compiling unit tests using DUB 
>>>> doesn't work.
>>>
>>> Source code?
>>
>> A common example from a textbook
>>
>> ```d
>> import std.array;
>>
>> bool binarySearch(T)(T[] input, T value)
>> {
>>     while (!input.empty)
>>     {
>>         auto i = input.length / 2;
>>         auto mid = input[i];
>>         if (mid > value)
>>             input = input[0 .. i];
>>         else if (mid < value)
>>             input = input[i + 1 .. $];
>>         else
>>             return true;
>>     }
>>     return false;
>> }
>>
>> unittest
>> {
>>     assert(binarySearch([1, 3, 6, 7, 9, 15], 7));
>>     assert(!binarySearch([1, 3, 6, 7, 9, 15], 5));
>> }
>> ```
> is there main()? if isn't it, please add the following line at 
> the end:
> ```d
> void main() {}
> ```

I managed to compile the file itself with this command:
```sh
dmd -main -unittest app.d
```
It was also possible to compile with the addition of the *main()* 
function to the project.

In the case of dub, I added entry to the project *"buildOptions": 
["unittests"]* and the file now looks like this:
```json
{
   "authors": [
     "alexander"
   ],
   "description": "Array Slicing",
   "license": "proprietary",
   "name": "array_slicing",
   "targetName": "program",
   "targetPath": "bin",
   "buildOptions": ["unittests"]
}
```
And when I call dub, I get this message
```sh
## Warning for package array_slicing ##

The following compiler flags have been specified in the package 
description
file. They are handled by DUB and direct use in packages is 
discouraged.
Alternatively, you can set the DFLAGS environment variable to 
pass custom flags
to the compiler, or use one of the suggestions below:

unittests: Call DUB with --build=unittest

Performing "debug" build using dmd for x86_64.
array_slicing ~master: building configuration "application"...
Linking...
Running bin/program
1 modules passed unittests
```
But as you can see, the unit test works. But how do I do all this 
right?



More information about the Digitalmars-d-learn mailing list