Skip to main content

kotlin_test

name

def name(
*,
name: str,
default_target_platform: None | str = ...,
target_compatible_with: list[str] = ...,
compatible_with: list[str] = ...,
exec_compatible_with: list[str] = ...,
visibility: list[str] = ...,
within_view: list[str] = ...,
metadata: OpaqueMetadata = ...,
tests: list[str] = ...,
modifiers: OpaqueMetadata = ...,
_apple_platforms: dict[str, str] = ...,
_build_only_native_code: bool = ...,
_exec_os_type: str = ...,
_inject_test_env: str = ...,
_is_building_android_binary: bool = ...,
_java_test_toolchain: str = ...,
_java_toolchain: str = ...,
_kotlin_toolchain: str = ...,
_remote_test_execution_toolchain: str = ...,
_test_toolchain: str = ...,
abi_generation_mode: None | str = ...,
annotation_processing_tool: None | str = ...,
annotation_processor_deps: list[str] = ...,
annotation_processor_params: list[str] = ...,
annotation_processors: list[str] = ...,
contacts: list[str] = ...,
cxx_library_allowlist: list[str] = ...,
default_cxx_platform: None | str = ...,
default_host_platform: None | str = ...,
deps: list[str] = ...,
deps_query: None | str = ...,
enable_used_classes: bool = ...,
env: dict[str, str] = ...,
exported_deps: list[str] = ...,
exported_provided_deps: list[str] = ...,
extra_arguments: list[str] = ...,
extra_kotlinc_arguments: list[str] = ...,
fork_mode: str = ...,
friend_paths: list[str] = ...,
incremental: bool = ...,
java: None | str = ...,
java_agents: list[str] = ...,
java_version: None | str = ...,
javac: None | str = ...,
k2: bool = ...,
kotlin_compiler_plugins: dict[str, dict[str, str]] = ...,
labels: list[str] = ...,
licenses: list[str] = ...,
manifest_file: None | str = ...,
maven_coords: None | str = ...,
never_mark_as_unused_dependency: None | bool = ...,
on_unused_dependencies: None | str = ...,
plugins: list[str] = ...,
proguard_config: None | str = ...,
provided_deps: list[str] = ...,
remote_execution: None | str | dict[str, None | bool | int | str | list[dict[str, str]] | dict[str, str | list[str]]] = ...,
remote_execution_action_key_providers: None | str = ...,
remove_classes: list[str] = ...,
required_for_source_only_abi: bool = ...,
resources: list[str] = ...,
resources_root: None | str = ...,
run_test_separately: bool = ...,
runtime_deps: list[str] = ...,
source: None | str = ...,
source_abi_verification_mode: None | str = ...,
source_only_abi_deps: list[str] = ...,
srcs: list[str] = ...,
std_err_log_level: None | int | str = ...,
std_out_log_level: None | int | str = ...,
target: None | str = ...,
test_case_timeout_ms: None | int = ...,
test_class_names_file: None | str = ...,
test_rule_timeout_ms: None | int = ...,
test_type: None | str = ...,
unbundled_resources_root: None | str = ...,
use_cxx_libraries: None | bool = ...,
use_dependency_order_classpath: None | bool = ...,
use_jvm_abi_gen: None | bool = ...,
vm_args: list[str] = ...,
) -> None

A kotlin_test() rule is used to define a set of .kt files that contain tests to run via JUnit.

Parameters

  • name: name of the target

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

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

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

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

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

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

  • metadata: a key-value map of metadata associated with this target

  • tests: a list of targets that provide tests for this one

  • modifiers: an array of modifiers associated with this target

  • cxx_library_allowlist: List of cxx_library targets to build, if use_cxx_libraries is true. This can be useful if some dependencies are Android-only and won't build for the test host platform.

  • deps: Same as kotlin_library(). // org.junit.rules.Timeout was not introduced until 4.7. Must include JUnit (version 4.7 or later) as a dependency for JUnit tests. Must include TestNG (version 6.2 or later) and hamcrest as a dependencies for TestNG tests.

  • enable_used_classes: Deprecated: for an experiment only, will be removed

  • env: A map of environment names and values to set when running the test.

  • fork_mode: Controls whether tests will all be run in the same process or a process will be started for each set of tests in a class.

    (This is mainly useful when porting Java tests to Buck from Apache Ant which allows JUnit tasks to set a fork="yes" property. It should not be used for new tests since it encourages tests to not cleanup after themselves and increases the tests' computational resources and running time.)

    none All tests will run in the same process. per_test A process will be started for each test class in which all tests of that test class will run.

  • incremental: Enables Kotlin incremental compilation.

  • javac: Specifies the Java compiler program to use for this rule. The value is a source path or an execution dep (e.g., //foo/bar:bar). Overrides the value in "javac" in the "tools" section of .buckconfig.

  • k2: Enables the Kotlin K2 compiler.

  • kotlin_compiler_plugins: Use this to specify Kotlin compiler plugins to use when compiling this library. This takes a map, with each entry specify one plugin. Entry's key is plugin source path, and value is a map of plugin option key value pair. Unlike extra_kotlinc_arguments, these can be source paths, not just strings.

    A special option value is __codegen_dir__, in which case Buck will provide a default codegen folder's path as option value instead. E.g.

    fbcode/buck2/prelude/decls/jvm_common.bzl
    kotlin_compiler_plugins = {
    "somePluginSourcePath": {
    "plugin:somePluginId:somePluginOptionKey": "somePluginOptionValue",
    "plugin:somePluginId:someDirectoryRelatedOptionKey": "__codegen_dir__",
    },
    },

    Each plugin source path will be prefixed with -Xplugin= and passed as extra arguments to the compiler. Plugin options will be appended after its plugin with -P.

    A specific example is, if you want to use kotlinx.serialization with kotlin_library(), you need to specify kotlinx-serialization-compiler-plugin.jar under kotlin_compiler_plugins and kotlinx-serialization-runtime.jar (which you may have to fetch from Maven) in your deps:


    kotlin_library(
    name = "example",
    srcs = glob(["*.kt"]),
    deps = [
    ":kotlinx-serialization-runtime",
    ],
    kotlin_compiler_plugins = {
    # Likely copied from your $KOTLIN_HOME directory.
    "kotlinx-serialization-compiler-plugin.jar": {},
    },
    )

    prebuilt_jar(
    name = "kotlinx-serialization-runtime",
    binary_jar = ":kotlinx-serialization-runtime-0.10.0",
    )

    # Note you probably want to set
    # maven_repo=http://jcenter.bintray.com/ in your .buckconfig until
    # https://github.com/Kotlin/kotlinx.serialization/issues/64
    # is closed.
    remote_file(
    name = "kotlinx-serialization-runtime-0.10.0",
    out = "kotlinx-serialization-runtime-0.10.0.jar",
    url = "mvn:org.jetbrains.kotlinx:kotlinx-serialization-runtime:jar:0.10.0",
    sha1 = "23d777a5282c1957c7ce35946374fff0adab114c"
    )

  • labels: 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

  • resources: Same as kotlin_library().

  • srcs: Like kotlin_library(), all of the .kt files specified by the srcs argument will be compiled when this rule is built. In addition, all of the corresponding .class files that are built by this rule will be passed as arguments to JUnit when this rule is run as a test. .class files that are passed to JUnit that do not have any methods annotated with @Test are considered failed tests, so make sure that only test case classes are specified as srcs. This is frequently done by specifying srcs as glob(['**/*Test.kt']).

  • std_err_log_level: Same as std_out_log_level, but for std err.

  • std_out_log_level: Log level for messages from the source under test that buck will output to std out. Value must be a valid java.util.logging.Level value.

  • test_rule_timeout_ms: 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 .

  • test_type: Specifies which test framework to use. The currently supported options are 'junit' and 'testng'.

  • vm_args: Runtime arguments to the JVM running the tests.