www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Unittesting libraries

reply simendsjo <simendsjo gmail.com> writes:
Is it possible to run unittests in libraries?
The following doesn't work:

l.d
===
module l;
import std.stdio;
int f() { return 1; } // just to make sure it's actually compiled in
unittest {
     writeln("Unittest from lib");
     assert(false);
}


t.d
===
import l;
import std.stdio;
void main() {
     writeln(f());
}


 dmd -unittest -lib l
 dmd -unittest t l.lib
 t.exe
1
Aug 08 2011
parent reply simendsjo <simendsjo gmail.com> writes:
On 08.08.2011 14:11, simendsjo wrote:
 Is it possible to run unittests in libraries?
 The following doesn't work:

 l.d
 ===
 module l;
 import std.stdio;
 int f() { return 1; } // just to make sure it's actually compiled in
 unittest {
 writeln("Unittest from lib");
 assert(false);
 }


 t.d
 ===
 import l;
 import std.stdio;
 void main() {
 writeln(f());
 }


  > dmd -unittest -lib l
  > dmd -unittest t l.lib
  > t.exe
 1
Anyone? Now I have to compile every source file from the library into t.d instead of compiling in the library.
Aug 10 2011
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Wednesday, August 10, 2011 09:11:53 simendsjo wrote:
 On 08.08.2011 14:11, simendsjo wrote:
 Is it possible to run unittests in libraries?
 The following doesn't work:
 
 l.d
 ===
 module l;
 import std.stdio;
 int f() { return 1; } // just to make sure it's actually compiled in
 unittest {
 writeln("Unittest from lib");
 assert(false);
 }
 
 
 t.d
 ===
 import l;
 import std.stdio;
 void main() {
 writeln(f());
 }
 
  > dmd -unittest -lib l
  > dmd -unittest t l.lib
  > t.exe
 
 1
Anyone? Now I have to compile every source file from the library into t.d instead of compiling in the library.
I don't know exactly what the deal with unit tests and libraries is, but it wouldn't surprise me at all if you have to compile a module into a binary to run its unit tests. Phobos (on Linux at least) is set up to build each module separately to run its unit tests. I can see why you'd want to do this, so it may be worth creating a bug report or enhancement request for it, but I have no idea whether it's supposed to work or not. - Jonathan M Davis
Aug 10 2011
parent reply simendsjo <simendsjo gmail.com> writes:
On 10.08.2011 09:29, Jonathan M Davis wrote:
 On Wednesday, August 10, 2011 09:11:53 simendsjo wrote:
 On 08.08.2011 14:11, simendsjo wrote:
 Is it possible to run unittests in libraries?
 The following doesn't work:

 l.d
 ===
 module l;
 import std.stdio;
 int f() { return 1; } // just to make sure it's actually compiled in
 unittest {
 writeln("Unittest from lib");
 assert(false);
 }


 t.d
 ===
 import l;
 import std.stdio;
 void main() {
 writeln(f());
 }

   >  dmd -unittest -lib l
   >  dmd -unittest t l.lib
   >  t.exe

 1
Anyone? Now I have to compile every source file from the library into t.d instead of compiling in the library.
I don't know exactly what the deal with unit tests and libraries is, but it wouldn't surprise me at all if you have to compile a module into a binary to run its unit tests. Phobos (on Linux at least) is set up to build each module separately to run its unit tests. I can see why you'd want to do this, so it may be worth creating a bug report or enhancement request for it, but I have no idea whether it's supposed to work or not. - Jonathan M Davis
I haven't tested, but does this mean invariants and contracts is not run too? In that case I think it should be fixed (if I'm not doing anything wrong).
Aug 10 2011
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Wednesday, August 10, 2011 12:01:00 simendsjo wrote:
 On 10.08.2011 09:29, Jonathan M Davis wrote:
 On Wednesday, August 10, 2011 09:11:53 simendsjo wrote:
 On 08.08.2011 14:11, simendsjo wrote:
 Is it possible to run unittests in libraries?
 The following doesn't work:
 
 l.d
 ===
 module l;
 import std.stdio;
 int f() { return 1; } // just to make sure it's actually compiled in
 unittest {
 writeln("Unittest from lib");
 assert(false);
 }
 
 
 t.d
 ===
 import l;
 import std.stdio;
 void main() {
 writeln(f());
 }
 
   >  dmd -unittest -lib l
   >  dmd -unittest t l.lib
   >  t.exe
 
 1
Anyone? Now I have to compile every source file from the library into t.d instead of compiling in the library.
I don't know exactly what the deal with unit tests and libraries is, but it wouldn't surprise me at all if you have to compile a module into a binary to run its unit tests. Phobos (on Linux at least) is set up to build each module separately to run its unit tests. I can see why you'd want to do this, so it may be worth creating a bug report or enhancement request for it, but I have no idea whether it's supposed to work or not. - Jonathan M Davis
I haven't tested, but does this mean invariants and contracts is not run too? In that case I think it should be fixed (if I'm not doing anything wrong).
They definitely should be. There's a big difference between running the unit tests and compiling in invariants and contracts. It could be that having built your library with -unittest, all of the unit tests are compiled in. They're just not run, because that requires some instrumenting from the runtime to run them before main. If it doesn't put the unit tests from the library in the list, then they won't be run. However, invariants and contracts should be compiled directly into the code such that there should be no need for separate code to call them like there is with the unit tests. Regardless, not running the unit tests from a library may be by design, but it is _definitely_ a bug if the invariants or contracts aren't run. But since people have hit invariants in Phobos when running their own programs (though that requires Phobos to have been compiled without -release), I'd be very surprised if it invariants or contracts were broken with regard to libraries. - Jonathan M Davis
Aug 10 2011
parent simendsjo <simendsjo gmail.com> writes:
On 10.08.2011 12:11, Jonathan M Davis wrote:
 On Wednesday, August 10, 2011 12:01:00 simendsjo wrote:
 On 10.08.2011 09:29, Jonathan M Davis wrote:
 On Wednesday, August 10, 2011 09:11:53 simendsjo wrote:
 On 08.08.2011 14:11, simendsjo wrote:
 Is it possible to run unittests in libraries?
 The following doesn't work:

 l.d
 ===
 module l;
 import std.stdio;
 int f() { return 1; } // just to make sure it's actually compiled in
 unittest {
 writeln("Unittest from lib");
 assert(false);
 }


 t.d
 ===
 import l;
 import std.stdio;
 void main() {
 writeln(f());
 }

    >   dmd -unittest -lib l
    >   dmd -unittest t l.lib
    >   t.exe

 1
Anyone? Now I have to compile every source file from the library into t.d instead of compiling in the library.
I don't know exactly what the deal with unit tests and libraries is, but it wouldn't surprise me at all if you have to compile a module into a binary to run its unit tests. Phobos (on Linux at least) is set up to build each module separately to run its unit tests. I can see why you'd want to do this, so it may be worth creating a bug report or enhancement request for it, but I have no idea whether it's supposed to work or not. - Jonathan M Davis
I haven't tested, but does this mean invariants and contracts is not run too? In that case I think it should be fixed (if I'm not doing anything wrong).
They definitely should be. There's a big difference between running the unit tests and compiling in invariants and contracts. It could be that having built your library with -unittest, all of the unit tests are compiled in. They're just not run, because that requires some instrumenting from the runtime to run them before main. If it doesn't put the unit tests from the library in the list, then they won't be run. However, invariants and contracts should be compiled directly into the code such that there should be no need for separate code to call them like there is with the unit tests. Regardless, not running the unit tests from a library may be by design, but it is _definitely_ a bug if the invariants or contracts aren't run. But since people have hit invariants in Phobos when running their own programs (though that requires Phobos to have been compiled without -release), I'd be very surprised if it invariants or contracts were broken with regard to libraries. - Jonathan M Davis
I added it to druntime as you think that's where the problem is. We'll see what Sean or others say about it. http://d.puremagic.com/issues/show_bug.cgi?id=6464
Aug 10 2011