Run a test component

This document describes how to run a test component using the Fuchsia emulator (FEMU).

Note: This guide is specific to components v2.

A component instance is started in Fuchsia when another component requests a capability from it. For example, the test manager (which is a component) starts a test component in response to a request to run a test.

This guide uses the hello-world-bin-test component in the basic example package. When you run this package’s hello-world-tests test suite, the test manager starts the hello-world-bin-test component. As a result, the component’s test binary runs on a Fuchsia device, or in this case, on the Fuchsia emulator.

The steps to run a test component are:

Prerequisites

Before you can run this test component, you must:

Build a Fuchsia image

Configure and build your Fuchsia image to include the test component:

  1. To include a specific component, run the fx set command with the --with option:

    fx set core.qemu-x64 --with //examples/components/basic:hello-world-tests
    

    //examples/components/basic is the directory of the example package and hello-world-tests is the name of the build target defined in the package's BUILD.gn file.

  2. Build your Fuchsia image:

    fx build
    

    When the fx build command completes, your new Fuchsia image now includes the hello-world-bin-test component, which can be fetched and launched on demand.

Start the emulator

Start the emulator with your Fuchsia image and run a package repository server:

Note: The steps in this section assume that you have set up and configured FEMU.

  1. In a new terminal, start the emulator:

    fx emu -N
    
  2. Set the emulator to be your device:

    fx set-device
    

    If you have multiple devices, select step-atom-yard-juicy (the emulator’s default device name), for example:

  3. In another new terminal, start a package repository server:

    fx serve
    

    Keep the fx serve command running as a package server for your device.

Run the test suite

Run the hello-world-tests test suite:

fx test hello-world-tests

This command prints the following output:

$ fx test hello-world-tests

...

[0/1] 00:00 🤔  /home/fuchsia/.jiri_root/bin/fx shell run-test-suite fuchsia-pkg://fuchsia.com/hello-world-tests#meta/hello-world-bin-test.cm
 >> Runtime has exceeded 2 seconds (adjust this value with the -s|--slow flag)
Running test 'fuchsia-pkg://fuchsia.com/hello-world-tests#meta/hello-world-bin-test.cm'

[RUNNING]   tests::assert_0_is_0
[PASSED]    tests::assert_0_is_0
1 out of 1 tests passed...
fuchsia-pkg://fuchsia.com/hello-world-tests#meta/hello-world-bin-test.cm completed with result: PASSED

[1/1] 00:05  /home/fuchsia/.jiri_root/bin/fx shell run-test-suite fuchsia-pkg://fuchsia.com/hello-world-tests#meta/hello-world-bin-test.cm

🎉  Ran 1 tests with 0 failures (use the -v flag to see each test) 🎉

The output shows that the hello-world-bin-test component is fetched from the package repository server and the component instance runs the test binary on the Fuchsia device (the emulator). See hello_world.rs for the source code of this test binary.