Skip to main content

go_test

A go_test() rule builds a native binary from the specified Go source and resource files—and a generated main file. It's similar to the go test command.

Details

If your test requires static files you should specify these in the resources argument. If you do not specify these files, they won't be available when your test runs.

Function Signature

def go_test(
*,
name: str,
default_target_platform: None | str = None,
target_compatible_with: list[str] = [],
compatible_with: list[str] = [],
exec_compatible_with: list[str] = [],
visibility: list[str] = [],
within_view: list[str] = ["PUBLIC"],
metadata: OpaqueMetadata = {},
tests: list[str] = [],
modifiers: OpaqueMetadata = [],
_apple_platforms: dict[str, str] = {},
_asan: bool = select({"prelude//go/constraints:asan_false": False, "prelude//go/constraints:asan_true": True, "DEFAULT": False}),
_build_info: dict[str, typing.Any] = {},
_build_tags: list[str] = [],
_coverage_mode: None | str = select({"prelude//go/constraints:coverage_mode_atomic": "atomic", "prelude//go/constraints:coverage_mode_count": "count", "prelude//go/constraints:coverage_mode_set": "set", "DEFAULT": None}),
_cxx_toolchain: str = "gh_facebook_buck2_shims_meta//:cxx",
_exec_os_type: str = "prelude//os_lookup/targets:os_lookup",
_go_stdlib: str = "prelude//go/tools:stdlib",
_go_toolchain: str = "gh_facebook_buck2_shims_meta//:go",
_inject_test_env: str = "prelude//test/tools:inject_test_env",
_race: bool = select({"prelude//go/constraints:race_false": False, "prelude//go/constraints:race_true": True, "DEFAULT": False}),
_remote_test_execution_toolchain: str = "gh_facebook_buck2_shims_meta//:remote_test_execution",
_test_toolchain: str = "gh_facebook_buck2_shims_meta//:test",
_testmaingen: str = "prelude//go_bootstrap/tools:go_testmaingen",
asan: bool = False,
assembler_flags: list[str] = [],
build_tags: list[str] = [],
cgo_enabled: None | bool = None,
compiler_flags: list[str] = [],
contacts: list[str] = [],
coverage_mode: None | str = None,
cxx_compiler_flags: list[str] = [],
cxx_preprocessor_flags: list[str] = [],
default_host_platform: None | str = None,
deps: list[str] = [],
embedcfg: None | str = None,
env: dict[str, str] = {},
external_linker_flags: list[str] = [],
header_namespace: None | str = None,
headers: list[str] | dict[str, str] = [],
labels: list[str] = [],
licenses: list[str] = [],
link_mode: None | str = None,
link_style: None | str = None,
linker_flags: list[str] = [],
package_name: None | str = None,
package_root: None | str = None,
platform: None | str = None,
race: bool = False,
remote_execution: None | str | dict[str, None | bool | int | str | list[dict[str, str]] | dict[str, str | list[str]]] = None,
resources: list[str] = [],
run_test_separately: bool = False,
runner: None | str = None,
specs: None | str = None,
srcs: list[str] = [],
target_under_test: None | str = None,
test_rule_timeout_ms: None | int = None,
) -> None

Parameters

  • name: (required)

    name of the target

  • default_target_platform: (defaults to: None)

    specifies the default target platform, used when no platforms are specified on the command line

  • target_compatible_with: (defaults to: [])

    a list of constraints that are required to be satisfied for this target to be compatible with a configuration

  • compatible_with: (defaults to: [])

    a list of constraints that are required to be satisfied for this target to be compatible with a configuration

  • exec_compatible_with: (defaults to: [])

    a list of constraints that are required to be satisfied for this target to be compatible with an execution platform

  • visibility: (defaults to: [])

    a list of visibility patterns restricting what targets can depend on this one

  • within_view: (defaults to: ["PUBLIC"])

    a list of visibility patterns restricting what this target can depend on

  • metadata: (defaults to: {})

    a key-value map of metadata associated with this target

  • tests: (defaults to: [])

    a list of targets that provide tests for this one

  • modifiers: (defaults to: [])

    an array of modifiers associated with this target

  • _build_info: (defaults to: {})

    Build info that is passed along here will be late-stamped into a fb_build_info section on the output binary

  • asan: (defaults to: False)

    If true, enable ASAN.

  • assembler_flags: (defaults to: [])

    The set of additional assembler flags to pass to go tool asm.

  • build_tags: (defaults to: [])

    Build tags to apply to this target and its dependencies.

  • cgo_enabled: (defaults to: None)

    Analog of CGO_ENABLED env-var, applies to this target and its dependencies. If None it will depend on the availability of CXX toolchain.

  • compiler_flags: (defaults to: [])

    The set of additional compiler flags to pass to go tool compile.

  • cxx_compiler_flags: (defaults to: [])

    GCC/Clang flags to use when compiling any of the above C/C++ sources (which require compilation).

  • cxx_preprocessor_flags: (defaults to: [])

    GCC/Clang flags to use when preprocessing any of the above C/C++ sources (which require preprocessing).

  • deps: (defaults to: [])

    The set of dependencies of this rule. Currently, this only supports go_library rules.

  • env: (defaults to: {})

    A map of environment variables and values to set when running the test.

  • external_linker_flags: (defaults to: [])

    Extra external linker flags passed to go link via -extld argument.

  • header_namespace: (defaults to: None)

    A path prefix when including headers of this target. Defaults to the path from the root of the repository to the directory where this target is defined. Can contain forward slashes (/), but cannot start with one. See headers for more information.

  • headers: (defaults to: [])

    The set of header files that are made available for inclusion to the source files in this target. These should be specified as either a list of header files or a dictionary of header names to header files. The header name can contain forward slashes (/). The headers can be included with #include "$HEADER_NAMESPACE/$HEADER_NAME" or #include <$HEADER_NAMESPACE/$HEADER_NAME> , where $HEADER_NAMESPACE is the value of the target's header_namespace attribute, and $HEADER_NAME is the header name if specified, and the filename of the header file otherwise. See header_namespace for more information.

  • labels: (defaults to: [])

    A list of labels to be applied to these tests. These labels are arbitrary text strings and have no meaning within buck itself. They can, however, have meaning for you as a test author (e.g., smoke or fast). A label can be used to filter or include a specific test rule when executing buck test

  • link_mode: (defaults to: None)

    Determines the link mode (equivalent of -mode). Can be one of the following values: internal, external. If no value is provided, the mode is set automatically depending on the other args.

  • link_style: (defaults to: None)

    Determines whether to build and link this rule's dependencies statically or dynamically. Can be one of the following values: static, static_pic or shared. This argument is relevant only if the cgo extension is enabled. Otherwise, Buck ignores this argument.

  • linker_flags: (defaults to: [])

    Extra linker flags passed to go link

  • package_name: (defaults to: None)

    Sets the full name of the test package being compiled. This defaults to the path from the buck root with "_test" appended. (e.g. given a ./.buckconfig, a rule in ./a/b/BUCK defaults to package "a/b_test")

    Note: if you want to test packages internally (i.e. same package name), use the target_under_test parameter instead of setting package_name to include the tested source files.

  • package_root: (defaults to: None)

    Sets Go package direactory (relative to BUCK file). By default (or if None passes) package_root is being detected automatically. Empty string of Go package is on the same level as BUCK file otherwise the subdirectory name. Example for srcs = ["foo/bar.go"], package_root = "foo"

  • race: (defaults to: False)

    If true, enable data race detection.

  • srcs: (defaults to: [])

    The set of source files to be compiled by this rule. .go files will be compiled with the Go compiler, .s files will be compiled with the assembler, and everything else is assumed to be files that may be #included by the assembler.

  • target_under_test: (defaults to: None)

    Specify the target_under_test that this internal test is testing. This will copy the srcs, package_name and deps from the target specified so you don't have to duplicate them.

  • test_rule_timeout_ms: (defaults to: None)

    If set specifies the maximum amount of time (in milliseconds) in which all of the tests in this rule should complete. This overrides the default rule_timeout if any has been specified in .buckconfig .

Examples

For more examples, check out our integration tests.



go_library(
name='greeting',
srcs=[
'greeting.go',
],
deps=[
':join',
],
)

go_test(
name='greeting-test',
srcs=[
'greeting_ext_test.go',
],
deps=[
':greeting'
],
)

go_test(
name='greeting-internal-test',
package_name='greeting',
srcs=[
'greeting.go',
'greeting_test.go',
],
deps=[
':join',
],
)

# Or

go_test(
name='greeting-better-internal-test',
srcs=['greeting_test.go'],
target_under_test=':greeting',
)