Host-side Python tests in Fuchsia are executed via `fx test`, CI, and CQ using the Fuchsia-vendored Python interpreter (`fuchsia-vendored-python`).

## Defining a Host Test

Use the [`python_host_test`](https://cs.opensource.google/fuchsia/fuchsia/+/main:/build/python/python_host_test.gni) GN template to define a test target.

Test `.py` files must use the standard shebang:

    #!/usr/bin/env fuchsia-vendored-python

### Example `BUILD.gn`

    import("//build/python/python_host_test.gni")

    if (is_host) {
      python_host_test("my_script_test") {
        main_source = "my_script_test.py"
        sources = [ "test_helper.py" ]
        libraries = [ "//path/to/library:lib" ]
      }
    }

    group("tests") {
      testonly = true
      public_deps = [ ":my_script_test($host_toolchain)" ]
    }

## Key GN Parameters

- `main_source`: (Required) Path to the primary `.py` test entry point.
- `libraries`: (Optional) List of `python_library` labels imported by the test.
- `enable_mypy`: (Optional) Enables MyPy static type checking. Defaults to `true`.

## Running Tests

Run tests locally using `fx test`:

    fx test //path/to/my_test:my_script_test

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