C++ unit testing using a scripting language
I would like to utilize some scripting language in order to perform unit testing of the C++ code. It could be easier to develop tests in a scripting language given that it has access to the required C开发者_运维百科++ functions.
I have either Ruby or Perl in mind (because I am familiar with them). It seems that I could use SWIG to interface with the C++ code.
Are there any better alternatives to SWIG? What scripting language would you personally use for this purpose?
And finally, is it appropriate (and efficient) to use this approach to testing?
Inline:CPP provides a relatively simple means of calling C++ code from Perl.
I recommend running your script using the following (or with an equivalent directive inside of the test script):
perl -MInline=FORCE,NOISY,NOCLEAN test.pl
If you want to test your C++ code in a similar manner to how you would test Perl code, you could use a library designed to output TAP.
Looking at the page listing the TAP producers There appears to be only one designed specifically for C++.
You may also want to look at the GitHub page for libtap++.
#include <tap++.h>
#include <string>
using namespace TAP;
int foo() {
return 1;
}
std::string bar() {
return "a string";
}
int main() {
plan(3);
ok(true, "This test passes");
is(foo(), 1, "foo() should be 1");
is(bar(), "a string", "bar() should be \"a string\"");
return exit_status();
}
Which produces something like:
1..3 ok 1 - This test passes ok 2 - foo() should be 1 ok 3 - bar() should be "a string"
Which can be parsed by prove provided by Test::Harness.
The best part is you shouldn't have to learn a language other than the one you are already using.
If you're writing C code and compiling it with the C++ compiler, using another language for testing can work.
But not for code written in a C++ style.
How do you test for correct and leak-free copy-construction, assignment, overloaded operators? What about templates? No scripting language is going to call these in the same way C++ does, many don't even have a concept of pass-by-value.
Test from the environment you're going to ultimately use to consume the code.
精彩评论