- Qbs Manual
- Autotest
- Qbs 2.4.0

Convenience Items ◦ Project Properties

# 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

     MyApplication {
         type: base.concat(["autotest"])
     }

This additional type is required for the helper AutotestRunner item. This item runs all products with the `"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
         }
     }

Here we set the timeout property to 60 seconds so that our runner kills tests that run way too long.

Now we need to add our first test. Let's create a new product with the following content:

     // test/test.qbs

     MyAutoTest {
         Depends { name: "mylib" }
         name: "mytest"
         files: "test.c"
     }

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:

     // test/test.c

     #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;
     }

The test compares the value from the library with the value from the command line.

Don't forget to add the new test product to the references property in the Project:

     references: [
         "app/app.qbs",
         "lib/lib.qbs",
         "test/test.qbs",
     ]

Now we can build the autotest product - this will also launch our test:

     $ qbs build -p "autotest-runner"
     ...
     running test mytest [autotest-runner]
     Build done for configuration default.

Convenience Items ◦ Project Properties