6. Example test code

Here is a minimal dummy example of a KTF unit test suite that defines two tests, hello_ok and hello_fail. The test is in the examples directory and is built with KTF:

// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
 *    Author: Knut Omang <knut.omang@oracle.com>
 */

#include <linux/module.h>
#include "ktf.h"

MODULE_LICENSE("GPL");

KTF_INIT();

TEST(examples, hello_ok)
{
	EXPECT_TRUE(true);
}

TEST(examples, hello_fail)
{
	EXPECT_TRUE(false);
}

static void add_tests(void)
{
	ADD_TEST(hello_ok);
	ADD_TEST(hello_fail);
}

static int __init hello_init(void)
{
	add_tests();
	tlog(T_INFO, "hello: loaded");
	return 0;
}

static void __exit hello_exit(void)
{
	KTF_CLEANUP();
	tlog(T_INFO, "hello: unloaded");
}

module_init(hello_init);
module_exit(hello_exit);

To run the test, cd to your KTF build tree and insmod the ktf module and the module that provides the test:

insmod kernel/ktf.ko
insmod examples/hello.ko

Now you should be able to run one or more of the tests by running the application ktfrun built in user/ktfrun. You should be able to run that application as an ordinary user:

ktfrun --gtest_list_tests
ktfrun --gtest_filter='*fail'
ktfrun --gtest_filter='*ok'

There are more examples in the examples directory. KTF also includes a selftest directory used to test/check the KTF implementation itself.