This guide shows you how to build Fuchsia to include a test package from
Fuchsia's source //examples
directory and run tests on
your Fuchsia target.
Prerequisites
Before you can run this test component, you must:
Exploring the example
The Hello, world!
examples includes unit test components written in the
various supported languages. A test component has two key elements:
- An executable test suite written in a supported language.
- A
BUILD.gn
file to define the test component build target and include it in a Fuchsia test package.
Executable test suite
Fuchsia test suites are built as components and execute within a test runner for supported testing frameworks, such as GoogleTest. The test suite binary includes test cases written against these language-specific frameworks.
C++
#include <gtest/gtest.h>
TEST(HelloWorldTest, True) { EXPECT_TRUE(true); }
Rust
#[cfg(test)]
mod tests {
#[fuchsia::test]
fn it_works() {
assert_eq!(true, true);
}
}
For more details on testing Fuchsia components, see Testing with Components
BUILD.gn
The BUILD.gn
file declares build targets for fuchsia_unittest_package()
.
This template is specifically designed to package components containing tests.
C++
executable("unittests") {
testonly = true
output_name = "hello_world_cpp_test_bin"
sources = [ "hello_world_unittest.cc" ]
deps = [ "//src/lib/fxl/test:gtest_main" ]
}
fuchsia_unittest_package("hello-world-cpp-unittests") {
deps = [ ":unittests" ]
}
Rust
rustc_binary("bin") {
name = "hello_world_rust"
# Generates the "bin_test" build target
with_unit_tests = true
edition = "2021"
deps = []
test_deps = [ "//src/lib/fuchsia" ]
sources = [ "src/main.rs" ]
}
fuchsia_unittest_package("hello-world-rust-tests") {
deps = [ ":bin_test" ]
}
To learn more about how Fuchsia uses GN to define test packages, see: Building components.
Include the example tests in your Fuchsia image
To include the example package in your build configuration, use the --with
flag
when setting your product and board environment:
fx set product.board --with //examples/hello_world:tests
For a Fuchsia emulator with the minimum build configuration, the command is:
fx set core.x64 --with //examples/hello_world:tests
In this example, core
is a product with a minimal feature set, which includes
common network capabilities, and x64
refers to the x64 architecture.
For a Fuchsia device with the minimum build configuration, the command is:
fx set core.x64 --with //examples/hello_world:tests
See Configure a build for more options.
Once you have set your build configuration, build Fuchsia with the following command:
fx build
You now have a build that includes the example tests that can be fetched and launched on demand.
Run the test suite
Open a terminal and run
fx serve
:fx serve
Open another terminal and run the
hello-world
unit tests:C++
fx test hello-world-cpp-unittests
Rust
fx test hello-world-rust-tests
This command prints the following output:
C++
$ fx test hello-world-cpp-unittests
...
PASS: 0 FAIL: 0 00:00 🤔 fx shell run-test-suite '--max-severity-logs WARN' fuchsia-pkg://fuchsia.com/hello-world-cpp-unittests?hash=c34ed8b2ea21fd5158f1de77a5581a4b5123161ef851eea430768a00efc1cbbf#meta/hello-world-cpp-unittests.cm
>> Runtime has exceeded 2 seconds (adjust this value with the -s|--slow flag)
Running test 'fuchsia-pkg://fuchsia.com/hello-world-cpp-unittests?hash=c34ed8b2ea21fd5158f1de77a5581a4b5123161ef851eea430768a00efc1cbbf#meta/hello-world-cpp-unittests.cm'
[RUNNING] HelloWorldTest.True
[PASSED] HelloWorldTest.True
1 out of 1 tests passed...
fuchsia-pkg://fuchsia.com/hello-world-cpp-unittests?hash=c34ed8b2ea21fd5158f1de77a5581a4b5123161ef851eea430768a00efc1cbbf#meta/hello-world-cpp-unittests.cm completed with result: PASSED
PASS: 1 FAIL: 0 00:15 ✅ fx shell run-test-suite '--max-severity-logs WARN' fuchsia-pkg://fuchsia.com/hello-world-cpp-unittests?hash=c34ed8b2ea21fd5158f1de77a5581a4b5123161ef851eea430768a00efc1cbbf#meta/hello-world-cpp-unittests.cm
🎉 Ran 1 tests with 0 failures (use the -v flag to see each test) 🎉
Rust
$ fx test hello-world-rust-tests
...
PASS: 0 FAIL: 0 00:00 🤔 fx shell run-test-suite '--max-severity-logs WARN' fuchsia-pkg://fuchsia.com/hello-world-rust-tests?hash=45cc85adaa09af18c575c45be942d72e173719c53e69d879eeb9602fa38e4884#meta/hello-world-rust-tests.cm
>> Runtime has exceeded 2 seconds (adjust this value with the -s|--slow flag)
Running test 'fuchsia-pkg://fuchsia.com/hello-world-rust-tests?hash=45cc85adaa09af18c575c45be942d72e173719c53e69d879eeb9602fa38e4884#meta/hello-world-rust-tests.cm'
[RUNNING] hello_tests::greeting_test
[RUNNING] hello_tests::my_test
[RUNNING] tests::it_works
[PASSED] tests::it_works
[PASSED] hello_tests::my_test
[PASSED] hello_tests::greeting_test
3 out of 3 tests passed...
fuchsia-pkg://fuchsia.com/hello-world-rust-tests?hash=45cc85adaa09af18c575c45be942d72e173719c53e69d879eeb9602fa38e4884#meta/hello-world-rust-tests.cm completed with result: PASSED
PASS: 1 FAIL: 0 00:15 ✅ fx shell run-test-suite '--max-severity-logs WARN' fuchsia-pkg://fuchsia.com/hello-world-rust-tests?hash=45cc85adaa09af18c575c45be942d72e173719c53e69d879eeb9602fa38e4884#meta/hello-world-rust-tests.cm