Build and test#
The running example is the component component_example of the module template. It consists of the following files
in score/component_example
and score/tests:
File |
Content |
|---|---|
|
Interface of the component |
|
Implementation |
|
Unit test |
|
Component integration test |
|
Bazel targets of the component |
|
Documentation of the component, see documentation generation |
|
Feature integration tests of the module in C++ and Rust |
The code#
The interface declares two functions:
1namespace score {
2namespace component_example {
3
4// Unit
5std::string make_hello();
6
7// Component API
8std::string make_hello_message(const std::string& name);
9
10} // namespace component_example
11} // namespace score
make_hello() stands for a unit, the smallest part of the component which is tested on its own.
make_hello_message() is the component API, which other components and modules use.
The implementation combines both:
1#include "score/component_example/src/component_example.hpp"
2
3namespace score {
4namespace component_example {
5
6std::string make_hello() {
7 return "Hello";
8}
9
10// req-traceability: comp_req__mod_temp_component_name__some_title
11std::string make_hello_message(const std::string& name) {
12 return make_hello() + ", " + name + "!";
13}
14
15} // namespace component_example
16} // namespace score
Line 10 links the implementation to the component requirement it implements. The
documentation generation chapter shows how this link appears in the documentation.
Include paths start at the root of the repository, e.g. score/component_example/src/....
Bazel targets#
The Bazel targets of the component are defined in score/component_example/BUILD:
1cc_library(
2 name = "component_example",
3 srcs = ["src/component_example.cpp"],
4 hdrs = ["src/component_example.hpp"],
5 visibility = ["//visibility:public"],
6)
7
8cc_test(
9 name = "component_example_unit_tests",
10 size = "small",
11 srcs = ["src/component_example_test.cpp"],
12 deps = [
13 ":component_example",
14 "@googletest//:gtest_main",
15 ],
16)
17
18cc_test(
19 name = "component_example_component_integration_tests",
20 size = "small",
21 srcs = ["tests/component_example_test.cpp"],
22 deps = [
23 ":component_example",
24 "@googletest//:gtest_main",
25 ],
26)
The cc_library
component_examplecontains the code of the component.visibility = ["//visibility:public"]allows other packages and modules to use it.The two cc_test targets build the tests with GoogleTest.
gtest_mainprovides themain()function, so the test files only contain the test cases.The
BUILDfile additionally contains thedocs_bundleof the component, which is explained in the documentation generation chapter.
Targets of other modules are referenced with the @<module>//<package>:<target> syntax, e.g.
@googletest//:gtest_main. To use an S-CORE module, declare it with bazel_dep in MODULE.bazel
(see First Eclipse S-CORE Module) and add its targets to deps, e.g. the CRC32 calculation of score_baselibs:
deps = [
"@score_baselibs//score/hash/code/crc:crc_ieee",
],
Test levels#
S-CORE distinguishes the test levels described in the software verification plan. The template contains an example for each of them:
Test level |
Location |
Framework |
Example |
|---|---|---|---|
Unit test |
|
GoogleTest |
|
Component integration test |
|
GoogleTest |
|
Feature integration test |
|
GoogleTest, Rust test |
|
The template uses the simplest form for all levels: plain GoogleTest tests for C++ and the built-in test framework for Rust. For more extensive tests, S-CORE provides dedicated frameworks: the test scenarios from testing_tools for component integration tests, which are implemented in C++ or Rust and driven by pytest, and the Integration Testing Framework (ITF) for feature integration tests, which runs pytest-based tests on Docker containers, QEMU virtual machines or real hardware. See Overview of technologies for details.
The component integration test shows how a test declares what it verifies:
1TEST(ComponentExampleTest, MakeHelloMessageReturnsExpectedGreeting) {
2 RecordProperty("PartiallyVerifies", "comp_req__mod_temp_component_name__some_title");
3 RecordProperty("TestType", "requirements-based");
4 RecordProperty("DerivationTechnique", "requirements-analysis");
5 RecordProperty("Description", "Verify make_hello_message returns the greeting for each name.");
6
7 EXPECT_EQ(make_hello_message("World"), "Hello, World!");
8 EXPECT_EQ(make_hello_message("SCORE"), "Hello, SCORE!");
9}
GoogleTest writes these properties into the test result. The documentation build reads them and links the test to the requirement, see documentation generation.
Toolchains and platforms#
Before building the binaries, it is helpful to introduce two fundamental Bazel concepts: toolchains and platforms.
A complete explanation is beyond the scope of this small tutorial, but the following brief overview is sufficient:
A Bazel toolchain specifies which compiler and linker are used (e.g., GCC, QCC, Ferrocene).
A Bazel platform defines the target CPU architecture and OS (e.g., x86_64 Linux or x86_64 QNX).
All S-CORE modules use the same toolchains, provided by
score_bazel_cpp_toolchains (GCC and QCC) and
score_toolchains_rust (Ferrocene),
and the platforms defined in score_bazel_platforms.
In the module template, they are registered in the MODULE.bazel block S-CORE Toolchains (DO NOT MODIFY), e.g.:
1gcc = use_extension("@score_bazel_cpp_toolchains//extensions:gcc.bzl", "gcc")
2
3gcc.toolchain(
4 name = "score_gcc_x86_64_toolchain",
5 target_cpu = "x86_64",
6 target_os = "linux",
7 use_default_package = True,
8 version = "12.2.0",
9)
The template contains such toolchains for Linux (x86_64, aarch64), QNX SDP 8.0 (x86_64, aarch64), Red Hat AutoSD and Elektrobit corbos Linux for Safety Applications. The file score.bazelrc/score_toolchain.bazelrc combines each platform with its toolchains into a build configuration:
1build:score-linux-x86_64 --config=_score_common
2build:score-linux-x86_64 --extra_toolchains=@score_gcc_x86_64_toolchain//:x86_64-linux-gcc_12.2.0
3build:score-linux-x86_64 --platforms=@score_bazel_platforms//:x86_64-linux-gcc_12.2.0-posix
4
5build:score-qnx-x86_64 --config=_score_common
6build:score-qnx-x86_64 --platforms=@score_bazel_platforms//:x86_64-qnx-sdp_8.0.0-posix
7build:score-qnx-x86_64 --extra_toolchains=@score_qcc_x86_64_toolchain//:x86_64-qnx-sdp_8.0.0
8build:score-qnx-x86_64 --extra_toolchains=@score_toolchains_rust//toolchains/ferrocene:ferrocene_x86_64_pc_nto_qnx800
Without --config, Bazel builds for the host with the LLVM toolchain that the template registers in MODULE.bazel
(toolchains_llvm). This is fine for a quick local test run; for the S-CORE target platforms, always use
one of the score-* configurations.
Tip
The CI/CD pipeline uses its own QNX license when building the code with QNX. If you want to build the source code with
the QNX compiler locally, you need a QNX license, e.g. the free community license QNX Everywhere, installed at
/opt/score_qnx/license/licenses, and access to the QNX SDP download. The setup is described in the
QNX integration guide
of score_bazel_cpp_toolchains.
Building and running the tests#
Build all targets of the module for Linux x86_64:
bazel build --config=score-linux-x86_64 //score/...
Run all tests of the module, or only the tests of one test level:
# all tests
bazel test --config=score-linux-x86_64 //score/...
# unit tests of the component
bazel test --config=score-linux-x86_64 //score/component_example:component_example_unit_tests
# component integration tests of the component
bazel test --config=score-linux-x86_64 //score/component_example:component_example_component_integration_tests
# feature integration tests
bazel test --config=score-linux-x86_64 //score/tests/...
The first run takes a while, because Bazel downloads the toolchains and dependencies. Afterwards, Bazel only
rebuilds what has changed and reuses the results of unchanged tests; such tests are marked as (cached)
in the test summary.
The results of each test, incl. the JUnit XML file test.xml, are stored in bazel-testlogs/, e.g.
bazel-testlogs/score/component_example/component_example_unit_tests/.
Checks before a pull request#
The CI/CD workflows of your repository run further checks on every pull request. Run them locally before you push:
Command |
Check |
|---|---|
|
Whitespace, copyright and REUSE headers, YAML formatting and the Bazel lockfile,
as configured in |
|
Formatting of the source code, e.g. C++, Rust, Python and Starlark; |
|
Copyright headers of the files in |
The next chapter shows how the component is documented and how requirements, code and tests are linked.