This guide shows you how to build Fuchsia to include a test package from
Fuchsia's source [`//examples`](https://cs.opensource.google/fuchsia/fuchsia/+/main:/examples/) directory and run tests on
your Fuchsia target.

> [!NOTE]
> **Note:** You can find the source code for the "Hello, World" example at [`//examples/hello_world`](https://cs.opensource.google/fuchsia/fuchsia/+/main:/examples/hello_world).

## Prerequisites

Before you can run this test component, you must:

- [Set up the Fuchsia development environment](https://fuchsia.dev/docs/get-started/get_fuchsia_source)

## 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](https://fuchsia.dev/fuchsia-src/development/run/run-test-component#executable-test) written in a supported language.
- A [`BUILD.gn`](https://fuchsia.dev/fuchsia-src/development/run/run-test-component#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](https://fuchsia.dev/docs/development/testing/components/test_runner_framework) 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](https://fuchsia.dev/docs/development/testing/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 = "2024"

      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](https://fuchsia.dev/docs/development/components/build).

## Include the example tests in your Fuchsia image

> [!NOTE]
> **Note:** For new build configurations, these commands can take up to 90 minutes.

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

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

See [Configure a build](https://fuchsia.dev/docs/development/build/fx#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](https://fuchsia.dev/docs/development/build/software_assembly/build_configuration#universe).

## Run the test suite

1. Open a terminal and run `fx serve`:

       fx serve

2. 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

[](https://issues.fuchsia.dev/issues/new?component=1439230&template=1881640&description=Issue+on:+https://fuchsia.dev/docs/development/run/run-test-component.md "Report a bug or suggest improvements on this page") [](https://cs.opensource.google/fuchsia/fuchsia/+/main:docs/development/run/run-test-component.md "View the source code of this page") [](https://ci.android.com/edit?repo=fuchsia/fuchsia/main&file=docs/development/run/run-test-component.md&cloneRepo=fuchsia/fuchsia:refs/heads/main&fullPath=fuchsia/docs/development/run/run-test-component.md "Edit the source code of this page")