Autotest
Now that we can re-use our base items, let's add a simple autotest. We inherit the new item
from the MyApplication
item and add an "autotest"
type:
qbs/imports/MyAutoTest.qbs
This additional type is required for the helper AutotestRunner item. This item runs all
products with the MyApplication {
type: base.concat(["autotest"])
}
"autotest"
type when it is being built. It also implicitly depends on all
such products, so they will be built before running as well.
Let's add this item to our top-level Project item:
Project {
name: "My Project"
minimumQbsVersion: "2.0"
// ...
AutotestRunner {
timeout: 60
}
}
Now we need to add our first test. Let's create a new product with the following content:
test/test.qbs
Here we depend on our library (which we are going to test), set the product
name, and specify the source file, which looks like this:
MyAutoTest {
Depends { name: "mylib" }
name: "mytest"
files: "test.c"
}
test/test.c
The test compares the value from the library with the value from the command line.
#include "lib.h"
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
if (argc > 2) {
printf("usage: test [value]\n");
return 1;
}
const char *expected = argc == 2 ? argv[1] : "Hello from library";
if (strcmp(get_string(), expected) != 0) {
printf("text differs\n");
return 1;
}
return 0;
}
Don't forget to add the new test product to the references property in the Project:
myproject.qbs
Now we can build the autotest product – this will also launch our test:
references: [
"app/app.qbs",
"lib/lib.qbs",
"test/test.qbs",
]
$ qbs build -p "autotest-runner"
...
running test mytest [autotest-runner]
Build done for configuration default.