Saving unit tests in separate files in D

I want to test my code written in D. I use DUB to create a project (but the configuration is pretty simple: just a name and dunit dependency).

In many projects, I have seen that unit tests are placed next to the actual code (e.g. http://wiki.dlang.org/Unittest#Placement ).

Although this is IMO ok for small modules and simple tests, what if I really wanted to test my code?

In Java (and other JVM languages), the convention is the exact opposite - to keep tests in separate files (usually mirroring the packaging of units under test).

Is it possible to have separate files for unit tests in D?

I like the classic setup:

 dub.json /source/mylib/app.d /tests/mylib/app_tests.d 

with the app_tests file (like other unter tests files) integrated with DUB - compiled / launched only during --unittests , etc.

+7
unit-testing testing d d2
source share
3 answers

I do not know about conventions, but recently I found such a solution for DUB:

 { "name": "sample", "description": "sample app", "configurations": [ { "name": "application", "targetType": "executable", }, { "name": "unittest", "targetType": "executable", "targetPath" : "tests", "buildOptions": ["unittests"], "excludedSourceFiles": ["source/app.d"], "sourcePaths": ["tests/"], "importPaths": ["tests/"], "dependencies": { "dunit": ">=1.0.9" } } ] } 

I found this idea in DUB sources . Now, if you run dub , the application will be created, and if you run dub test , unit tests (placed in tests/ ) will run.

This is not ideal, and I still have not worked, but it works for my simple needs.

One of the problems is that individual unit test modules do not have access to private elements in the sources (they can have access to the package and the public, though).

I'm not quite sure if there are any side effects of this configuration that I don't know about. Perhaps someone more experienced will test this approach.

+8
source share

There is nothing stopping you from having unittest blocks in one test code file from another module. You can put something in a unittest block, which you can put in a regular function. However, you will not be able to access any of the private members of the module under test, unless those unittest blocks are in the module under test. The usual access modifier restrictions apply.

There are people in the D community who don’t like having their unit tests next to what is being tested, and choosing to put tests in separate files, but D testing units were designed with the idea that you would put tests near the functions they are testing. This makes it much more obvious if you forget the unit test function and simplify its joint modification and testing. This is what the D standard library does, and AFAIK, this is what most D programmers prefer to do. IMHO, this is much better for servicing in this way, and if you think that it makes the module too large, either you probably make your modules too large, or you are too picky about how big the module is. But, obviously, this is subjective, and it's up to you to decide whether you want to put your tests in the same module as the ones they are testing. You just need to keep in mind that if they are separated, then they cannot gain access to any private members of the module under test.

+6
source share

Keep in mind that Java does not have modules (however, this will change in Java 9 when the Jigsaw project is finally merged) or built-in unittests. The story may be different if so. There are several good reasons why we keep unittests as close as possible to the piece of code we want to remove. The most obvious is the terrain. How many times have you had to switch between a test package and a regular package, where are your Java classes designed to analyze the method you are writing a block test for? :)

To answer your question - I see no reason why you cannot have unittests in a separate module. Just give it a try, this is what I will do if I were you.

+2
source share

All Articles