1*bcb5dc79SHONG Yifan<!-- Generated with Stardoc: http://skydoc.bazel.build --> 2*bcb5dc79SHONG Yifan 3*bcb5dc79SHONG YifanUnit testing support. 4*bcb5dc79SHONG Yifan 5*bcb5dc79SHONG YifanUnlike most Skylib files, this exports four modules: 6*bcb5dc79SHONG Yifan* `unittest` contains functions to declare and define unit tests for ordinary 7*bcb5dc79SHONG Yifan Starlark functions; 8*bcb5dc79SHONG Yifan* `analysistest` contains functions to declare and define tests for analysis 9*bcb5dc79SHONG Yifan phase behavior of a rule, such as a given target's providers or registered 10*bcb5dc79SHONG Yifan actions; 11*bcb5dc79SHONG Yifan* `loadingtest` contains functions to declare and define tests for loading 12*bcb5dc79SHONG Yifan phase behavior, such as macros and `native.*`; 13*bcb5dc79SHONG Yifan* `asserts` contains the assertions used within tests. 14*bcb5dc79SHONG Yifan 15*bcb5dc79SHONG YifanSee https://bazel.build/extending/concepts for background about macros, rules, 16*bcb5dc79SHONG Yifanand the different phases of a build. 17*bcb5dc79SHONG Yifan 18*bcb5dc79SHONG Yifan<a id="unittest_toolchain"></a> 19*bcb5dc79SHONG Yifan 20*bcb5dc79SHONG Yifan## unittest_toolchain 21*bcb5dc79SHONG Yifan 22*bcb5dc79SHONG Yifan<pre> 23*bcb5dc79SHONG Yifanunittest_toolchain(<a href="#unittest_toolchain-name">name</a>, <a href="#unittest_toolchain-escape_chars_with">escape_chars_with</a>, <a href="#unittest_toolchain-escape_other_chars_with">escape_other_chars_with</a>, <a href="#unittest_toolchain-failure_templ">failure_templ</a>, <a href="#unittest_toolchain-file_ext">file_ext</a>, 24*bcb5dc79SHONG Yifan <a href="#unittest_toolchain-join_on">join_on</a>, <a href="#unittest_toolchain-success_templ">success_templ</a>) 25*bcb5dc79SHONG Yifan</pre> 26*bcb5dc79SHONG Yifan 27*bcb5dc79SHONG Yifan 28*bcb5dc79SHONG Yifan 29*bcb5dc79SHONG Yifan**ATTRIBUTES** 30*bcb5dc79SHONG Yifan 31*bcb5dc79SHONG Yifan 32*bcb5dc79SHONG Yifan| Name | Description | Type | Mandatory | Default | 33*bcb5dc79SHONG Yifan| :------------- | :------------- | :------------- | :------------- | :------------- | 34*bcb5dc79SHONG Yifan| <a id="unittest_toolchain-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | | 35*bcb5dc79SHONG Yifan| <a id="unittest_toolchain-escape_chars_with"></a>escape_chars_with | Dictionary of characters that need escaping in test failure message to prefix appended to escape those characters. For example, `{"%": "%", ">": "^"}` would replace `%` with `%%` and `>` with `^>` in the failure message before that is included in `success_templ`. | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | `{}` | 36*bcb5dc79SHONG Yifan| <a id="unittest_toolchain-escape_other_chars_with"></a>escape_other_chars_with | String to prefix every character in test failure message which is not a key in `escape_chars_with` before including that in `success_templ`. For example, `""` would prefix every character in the failure message (except those in the keys of `escape_chars_with`) with `\`. | String | optional | `""` | 37*bcb5dc79SHONG Yifan| <a id="unittest_toolchain-failure_templ"></a>failure_templ | Test script template with a single `%s`. That placeholder is replaced with the lines in the failure message joined with the string specified in `join_with`. The resulting script should print the failure message and exit with non-zero status. | String | required | | 38*bcb5dc79SHONG Yifan| <a id="unittest_toolchain-file_ext"></a>file_ext | File extension for test script, including leading dot. | String | required | | 39*bcb5dc79SHONG Yifan| <a id="unittest_toolchain-join_on"></a>join_on | String used to join the lines in the failure message before including the resulting string in the script specified in `failure_templ`. | String | required | | 40*bcb5dc79SHONG Yifan| <a id="unittest_toolchain-success_templ"></a>success_templ | Test script generated when the test passes. Should exit with status 0. | String | required | | 41*bcb5dc79SHONG Yifan 42*bcb5dc79SHONG Yifan 43*bcb5dc79SHONG Yifan<a id="analysistest.begin"></a> 44*bcb5dc79SHONG Yifan 45*bcb5dc79SHONG Yifan## analysistest.begin 46*bcb5dc79SHONG Yifan 47*bcb5dc79SHONG Yifan<pre> 48*bcb5dc79SHONG Yifananalysistest.begin(<a href="#analysistest.begin-ctx">ctx</a>) 49*bcb5dc79SHONG Yifan</pre> 50*bcb5dc79SHONG Yifan 51*bcb5dc79SHONG YifanBegins an analysis test. 52*bcb5dc79SHONG Yifan 53*bcb5dc79SHONG YifanThis should be the first function called in an analysis test implementation 54*bcb5dc79SHONG Yifanfunction. It initializes a "test environment" that is used to collect 55*bcb5dc79SHONG Yifanassertion failures so that they can be reported and logged at the end of the 56*bcb5dc79SHONG Yifantest. 57*bcb5dc79SHONG Yifan 58*bcb5dc79SHONG Yifan 59*bcb5dc79SHONG Yifan**PARAMETERS** 60*bcb5dc79SHONG Yifan 61*bcb5dc79SHONG Yifan 62*bcb5dc79SHONG Yifan| Name | Description | Default Value | 63*bcb5dc79SHONG Yifan| :------------- | :------------- | :------------- | 64*bcb5dc79SHONG Yifan| <a id="analysistest.begin-ctx"></a>ctx | The Starlark context. Pass the implementation function's `ctx` argument in verbatim. | none | 65*bcb5dc79SHONG Yifan 66*bcb5dc79SHONG Yifan**RETURNS** 67*bcb5dc79SHONG Yifan 68*bcb5dc79SHONG YifanA test environment struct that must be passed to assertions and finally to 69*bcb5dc79SHONG Yifan`analysistest.end`. Do not rely on internal details about the fields in this 70*bcb5dc79SHONG Yifanstruct as it may change. 71*bcb5dc79SHONG Yifan 72*bcb5dc79SHONG Yifan 73*bcb5dc79SHONG Yifan<a id="analysistest.end"></a> 74*bcb5dc79SHONG Yifan 75*bcb5dc79SHONG Yifan## analysistest.end 76*bcb5dc79SHONG Yifan 77*bcb5dc79SHONG Yifan<pre> 78*bcb5dc79SHONG Yifananalysistest.end(<a href="#analysistest.end-env">env</a>) 79*bcb5dc79SHONG Yifan</pre> 80*bcb5dc79SHONG Yifan 81*bcb5dc79SHONG YifanEnds an analysis test and logs the results. 82*bcb5dc79SHONG Yifan 83*bcb5dc79SHONG YifanThis must be called and returned at the end of an analysis test implementation function so 84*bcb5dc79SHONG Yifanthat the results are reported. 85*bcb5dc79SHONG Yifan 86*bcb5dc79SHONG Yifan 87*bcb5dc79SHONG Yifan**PARAMETERS** 88*bcb5dc79SHONG Yifan 89*bcb5dc79SHONG Yifan 90*bcb5dc79SHONG Yifan| Name | Description | Default Value | 91*bcb5dc79SHONG Yifan| :------------- | :------------- | :------------- | 92*bcb5dc79SHONG Yifan| <a id="analysistest.end-env"></a>env | The test environment returned by `analysistest.begin`. | none | 93*bcb5dc79SHONG Yifan 94*bcb5dc79SHONG Yifan**RETURNS** 95*bcb5dc79SHONG Yifan 96*bcb5dc79SHONG YifanA list of providers needed to automatically register the analysis test result. 97*bcb5dc79SHONG Yifan 98*bcb5dc79SHONG Yifan 99*bcb5dc79SHONG Yifan<a id="analysistest.fail"></a> 100*bcb5dc79SHONG Yifan 101*bcb5dc79SHONG Yifan## analysistest.fail 102*bcb5dc79SHONG Yifan 103*bcb5dc79SHONG Yifan<pre> 104*bcb5dc79SHONG Yifananalysistest.fail(<a href="#analysistest.fail-env">env</a>, <a href="#analysistest.fail-msg">msg</a>) 105*bcb5dc79SHONG Yifan</pre> 106*bcb5dc79SHONG Yifan 107*bcb5dc79SHONG YifanUnconditionally causes the current test to fail. 108*bcb5dc79SHONG Yifan 109*bcb5dc79SHONG Yifan**PARAMETERS** 110*bcb5dc79SHONG Yifan 111*bcb5dc79SHONG Yifan 112*bcb5dc79SHONG Yifan| Name | Description | Default Value | 113*bcb5dc79SHONG Yifan| :------------- | :------------- | :------------- | 114*bcb5dc79SHONG Yifan| <a id="analysistest.fail-env"></a>env | The test environment returned by `unittest.begin`. | none | 115*bcb5dc79SHONG Yifan| <a id="analysistest.fail-msg"></a>msg | The message to log describing the failure. | none | 116*bcb5dc79SHONG Yifan 117*bcb5dc79SHONG Yifan 118*bcb5dc79SHONG Yifan<a id="analysistest.make"></a> 119*bcb5dc79SHONG Yifan 120*bcb5dc79SHONG Yifan## analysistest.make 121*bcb5dc79SHONG Yifan 122*bcb5dc79SHONG Yifan<pre> 123*bcb5dc79SHONG Yifananalysistest.make(<a href="#analysistest.make-impl">impl</a>, <a href="#analysistest.make-expect_failure">expect_failure</a>, <a href="#analysistest.make-attrs">attrs</a>, <a href="#analysistest.make-fragments">fragments</a>, <a href="#analysistest.make-config_settings">config_settings</a>, 124*bcb5dc79SHONG Yifan <a href="#analysistest.make-extra_target_under_test_aspects">extra_target_under_test_aspects</a>, <a href="#analysistest.make-doc">doc</a>) 125*bcb5dc79SHONG Yifan</pre> 126*bcb5dc79SHONG Yifan 127*bcb5dc79SHONG YifanCreates an analysis test rule from its implementation function. 128*bcb5dc79SHONG Yifan 129*bcb5dc79SHONG YifanAn analysis test verifies the behavior of a "real" rule target by examining 130*bcb5dc79SHONG Yifanand asserting on the providers given by the real target. 131*bcb5dc79SHONG Yifan 132*bcb5dc79SHONG YifanEach analysis test is defined in an implementation function that must then be 133*bcb5dc79SHONG Yifanassociated with a rule so that a target can be built. This function handles 134*bcb5dc79SHONG Yifanthe boilerplate to create and return a test rule and captures the 135*bcb5dc79SHONG Yifanimplementation function's name so that it can be printed in test feedback. 136*bcb5dc79SHONG Yifan 137*bcb5dc79SHONG YifanAn example of an analysis test: 138*bcb5dc79SHONG Yifan 139*bcb5dc79SHONG Yifan``` 140*bcb5dc79SHONG Yifandef _your_test(ctx): 141*bcb5dc79SHONG Yifan env = analysistest.begin(ctx) 142*bcb5dc79SHONG Yifan 143*bcb5dc79SHONG Yifan # Assert statements go here 144*bcb5dc79SHONG Yifan 145*bcb5dc79SHONG Yifan return analysistest.end(env) 146*bcb5dc79SHONG Yifan 147*bcb5dc79SHONG Yifanyour_test = analysistest.make(_your_test) 148*bcb5dc79SHONG Yifan``` 149*bcb5dc79SHONG Yifan 150*bcb5dc79SHONG YifanRecall that names of test rules must end in `_test`. 151*bcb5dc79SHONG Yifan 152*bcb5dc79SHONG Yifan 153*bcb5dc79SHONG Yifan**PARAMETERS** 154*bcb5dc79SHONG Yifan 155*bcb5dc79SHONG Yifan 156*bcb5dc79SHONG Yifan| Name | Description | Default Value | 157*bcb5dc79SHONG Yifan| :------------- | :------------- | :------------- | 158*bcb5dc79SHONG Yifan| <a id="analysistest.make-impl"></a>impl | The implementation function of the unit test. | none | 159*bcb5dc79SHONG Yifan| <a id="analysistest.make-expect_failure"></a>expect_failure | If true, the analysis test will expect the target_under_test to fail. Assertions can be made on the underlying failure using asserts.expect_failure | `False` | 160*bcb5dc79SHONG Yifan| <a id="analysistest.make-attrs"></a>attrs | An optional dictionary to supplement the attrs passed to the unit test's `rule()` constructor. | `{}` | 161*bcb5dc79SHONG Yifan| <a id="analysistest.make-fragments"></a>fragments | An optional list of fragment names that can be used to give rules access to language-specific parts of configuration. | `[]` | 162*bcb5dc79SHONG Yifan| <a id="analysistest.make-config_settings"></a>config_settings | A dictionary of configuration settings to change for the target under test and its dependencies. This may be used to essentially change 'build flags' for the target under test, and may thus be utilized to test multiple targets with different flags in a single build | `{}` | 163*bcb5dc79SHONG Yifan| <a id="analysistest.make-extra_target_under_test_aspects"></a>extra_target_under_test_aspects | An optional list of aspects to apply to the target_under_test in addition to those set up by default for the test harness itself. | `[]` | 164*bcb5dc79SHONG Yifan| <a id="analysistest.make-doc"></a>doc | A description of the rule that can be extracted by documentation generating tools. | `""` | 165*bcb5dc79SHONG Yifan 166*bcb5dc79SHONG Yifan**RETURNS** 167*bcb5dc79SHONG Yifan 168*bcb5dc79SHONG YifanA rule definition that should be stored in a global whose name ends in 169*bcb5dc79SHONG Yifan`_test`. 170*bcb5dc79SHONG Yifan 171*bcb5dc79SHONG Yifan 172*bcb5dc79SHONG Yifan<a id="analysistest.target_actions"></a> 173*bcb5dc79SHONG Yifan 174*bcb5dc79SHONG Yifan## analysistest.target_actions 175*bcb5dc79SHONG Yifan 176*bcb5dc79SHONG Yifan<pre> 177*bcb5dc79SHONG Yifananalysistest.target_actions(<a href="#analysistest.target_actions-env">env</a>) 178*bcb5dc79SHONG Yifan</pre> 179*bcb5dc79SHONG Yifan 180*bcb5dc79SHONG YifanReturns a list of actions registered by the target under test. 181*bcb5dc79SHONG Yifan 182*bcb5dc79SHONG Yifan**PARAMETERS** 183*bcb5dc79SHONG Yifan 184*bcb5dc79SHONG Yifan 185*bcb5dc79SHONG Yifan| Name | Description | Default Value | 186*bcb5dc79SHONG Yifan| :------------- | :------------- | :------------- | 187*bcb5dc79SHONG Yifan| <a id="analysistest.target_actions-env"></a>env | The test environment returned by `analysistest.begin`. | none | 188*bcb5dc79SHONG Yifan 189*bcb5dc79SHONG Yifan**RETURNS** 190*bcb5dc79SHONG Yifan 191*bcb5dc79SHONG YifanA list of actions registered by the target under test 192*bcb5dc79SHONG Yifan 193*bcb5dc79SHONG Yifan 194*bcb5dc79SHONG Yifan<a id="analysistest.target_bin_dir_path"></a> 195*bcb5dc79SHONG Yifan 196*bcb5dc79SHONG Yifan## analysistest.target_bin_dir_path 197*bcb5dc79SHONG Yifan 198*bcb5dc79SHONG Yifan<pre> 199*bcb5dc79SHONG Yifananalysistest.target_bin_dir_path(<a href="#analysistest.target_bin_dir_path-env">env</a>) 200*bcb5dc79SHONG Yifan</pre> 201*bcb5dc79SHONG Yifan 202*bcb5dc79SHONG YifanReturns ctx.bin_dir.path for the target under test. 203*bcb5dc79SHONG Yifan 204*bcb5dc79SHONG Yifan**PARAMETERS** 205*bcb5dc79SHONG Yifan 206*bcb5dc79SHONG Yifan 207*bcb5dc79SHONG Yifan| Name | Description | Default Value | 208*bcb5dc79SHONG Yifan| :------------- | :------------- | :------------- | 209*bcb5dc79SHONG Yifan| <a id="analysistest.target_bin_dir_path-env"></a>env | The test environment returned by `analysistest.begin`. | none | 210*bcb5dc79SHONG Yifan 211*bcb5dc79SHONG Yifan**RETURNS** 212*bcb5dc79SHONG Yifan 213*bcb5dc79SHONG YifanOutput bin dir path string. 214*bcb5dc79SHONG Yifan 215*bcb5dc79SHONG Yifan 216*bcb5dc79SHONG Yifan<a id="analysistest.target_under_test"></a> 217*bcb5dc79SHONG Yifan 218*bcb5dc79SHONG Yifan## analysistest.target_under_test 219*bcb5dc79SHONG Yifan 220*bcb5dc79SHONG Yifan<pre> 221*bcb5dc79SHONG Yifananalysistest.target_under_test(<a href="#analysistest.target_under_test-env">env</a>) 222*bcb5dc79SHONG Yifan</pre> 223*bcb5dc79SHONG Yifan 224*bcb5dc79SHONG YifanReturns the target under test. 225*bcb5dc79SHONG Yifan 226*bcb5dc79SHONG Yifan**PARAMETERS** 227*bcb5dc79SHONG Yifan 228*bcb5dc79SHONG Yifan 229*bcb5dc79SHONG Yifan| Name | Description | Default Value | 230*bcb5dc79SHONG Yifan| :------------- | :------------- | :------------- | 231*bcb5dc79SHONG Yifan| <a id="analysistest.target_under_test-env"></a>env | The test environment returned by `analysistest.begin`. | none | 232*bcb5dc79SHONG Yifan 233*bcb5dc79SHONG Yifan**RETURNS** 234*bcb5dc79SHONG Yifan 235*bcb5dc79SHONG YifanThe target under test. 236*bcb5dc79SHONG Yifan 237*bcb5dc79SHONG Yifan 238*bcb5dc79SHONG Yifan<a id="asserts.equals"></a> 239*bcb5dc79SHONG Yifan 240*bcb5dc79SHONG Yifan## asserts.equals 241*bcb5dc79SHONG Yifan 242*bcb5dc79SHONG Yifan<pre> 243*bcb5dc79SHONG Yifanasserts.equals(<a href="#asserts.equals-env">env</a>, <a href="#asserts.equals-expected">expected</a>, <a href="#asserts.equals-actual">actual</a>, <a href="#asserts.equals-msg">msg</a>) 244*bcb5dc79SHONG Yifan</pre> 245*bcb5dc79SHONG Yifan 246*bcb5dc79SHONG YifanAsserts that the given `expected` and `actual` values are equal. 247*bcb5dc79SHONG Yifan 248*bcb5dc79SHONG Yifan**PARAMETERS** 249*bcb5dc79SHONG Yifan 250*bcb5dc79SHONG Yifan 251*bcb5dc79SHONG Yifan| Name | Description | Default Value | 252*bcb5dc79SHONG Yifan| :------------- | :------------- | :------------- | 253*bcb5dc79SHONG Yifan| <a id="asserts.equals-env"></a>env | The test environment returned by `unittest.begin`. | none | 254*bcb5dc79SHONG Yifan| <a id="asserts.equals-expected"></a>expected | The expected value of some computation. | none | 255*bcb5dc79SHONG Yifan| <a id="asserts.equals-actual"></a>actual | The actual value returned by some computation. | none | 256*bcb5dc79SHONG Yifan| <a id="asserts.equals-msg"></a>msg | An optional message that will be printed that describes the failure. If omitted, a default will be used. | `None` | 257*bcb5dc79SHONG Yifan 258*bcb5dc79SHONG Yifan 259*bcb5dc79SHONG Yifan<a id="asserts.expect_failure"></a> 260*bcb5dc79SHONG Yifan 261*bcb5dc79SHONG Yifan## asserts.expect_failure 262*bcb5dc79SHONG Yifan 263*bcb5dc79SHONG Yifan<pre> 264*bcb5dc79SHONG Yifanasserts.expect_failure(<a href="#asserts.expect_failure-env">env</a>, <a href="#asserts.expect_failure-expected_failure_msg">expected_failure_msg</a>) 265*bcb5dc79SHONG Yifan</pre> 266*bcb5dc79SHONG Yifan 267*bcb5dc79SHONG YifanAsserts that the target under test has failed with a given error message. 268*bcb5dc79SHONG Yifan 269*bcb5dc79SHONG YifanThis requires that the analysis test is created with `analysistest.make()` and 270*bcb5dc79SHONG Yifan`expect_failures = True` is specified. 271*bcb5dc79SHONG Yifan 272*bcb5dc79SHONG Yifan 273*bcb5dc79SHONG Yifan**PARAMETERS** 274*bcb5dc79SHONG Yifan 275*bcb5dc79SHONG Yifan 276*bcb5dc79SHONG Yifan| Name | Description | Default Value | 277*bcb5dc79SHONG Yifan| :------------- | :------------- | :------------- | 278*bcb5dc79SHONG Yifan| <a id="asserts.expect_failure-env"></a>env | The test environment returned by `analysistest.begin`. | none | 279*bcb5dc79SHONG Yifan| <a id="asserts.expect_failure-expected_failure_msg"></a>expected_failure_msg | The error message to expect as a result of analysis failures. | `""` | 280*bcb5dc79SHONG Yifan 281*bcb5dc79SHONG Yifan 282*bcb5dc79SHONG Yifan<a id="asserts.false"></a> 283*bcb5dc79SHONG Yifan 284*bcb5dc79SHONG Yifan## asserts.false 285*bcb5dc79SHONG Yifan 286*bcb5dc79SHONG Yifan<pre> 287*bcb5dc79SHONG Yifanasserts.false(<a href="#asserts.false-env">env</a>, <a href="#asserts.false-condition">condition</a>, <a href="#asserts.false-msg">msg</a>) 288*bcb5dc79SHONG Yifan</pre> 289*bcb5dc79SHONG Yifan 290*bcb5dc79SHONG YifanAsserts that the given `condition` is false. 291*bcb5dc79SHONG Yifan 292*bcb5dc79SHONG Yifan**PARAMETERS** 293*bcb5dc79SHONG Yifan 294*bcb5dc79SHONG Yifan 295*bcb5dc79SHONG Yifan| Name | Description | Default Value | 296*bcb5dc79SHONG Yifan| :------------- | :------------- | :------------- | 297*bcb5dc79SHONG Yifan| <a id="asserts.false-env"></a>env | The test environment returned by `unittest.begin`. | none | 298*bcb5dc79SHONG Yifan| <a id="asserts.false-condition"></a>condition | A value that will be evaluated in a Boolean context. | none | 299*bcb5dc79SHONG Yifan| <a id="asserts.false-msg"></a>msg | An optional message that will be printed that describes the failure. If omitted, a default will be used. | `"Expected condition to be false, but was true."` | 300*bcb5dc79SHONG Yifan 301*bcb5dc79SHONG Yifan 302*bcb5dc79SHONG Yifan<a id="asserts.new_set_equals"></a> 303*bcb5dc79SHONG Yifan 304*bcb5dc79SHONG Yifan## asserts.new_set_equals 305*bcb5dc79SHONG Yifan 306*bcb5dc79SHONG Yifan<pre> 307*bcb5dc79SHONG Yifanasserts.new_set_equals(<a href="#asserts.new_set_equals-env">env</a>, <a href="#asserts.new_set_equals-expected">expected</a>, <a href="#asserts.new_set_equals-actual">actual</a>, <a href="#asserts.new_set_equals-msg">msg</a>) 308*bcb5dc79SHONG Yifan</pre> 309*bcb5dc79SHONG Yifan 310*bcb5dc79SHONG YifanAsserts that the given `expected` and `actual` sets are equal. 311*bcb5dc79SHONG Yifan 312*bcb5dc79SHONG Yifan**PARAMETERS** 313*bcb5dc79SHONG Yifan 314*bcb5dc79SHONG Yifan 315*bcb5dc79SHONG Yifan| Name | Description | Default Value | 316*bcb5dc79SHONG Yifan| :------------- | :------------- | :------------- | 317*bcb5dc79SHONG Yifan| <a id="asserts.new_set_equals-env"></a>env | The test environment returned by `unittest.begin`. | none | 318*bcb5dc79SHONG Yifan| <a id="asserts.new_set_equals-expected"></a>expected | The expected set resulting from some computation. | none | 319*bcb5dc79SHONG Yifan| <a id="asserts.new_set_equals-actual"></a>actual | The actual set returned by some computation. | none | 320*bcb5dc79SHONG Yifan| <a id="asserts.new_set_equals-msg"></a>msg | An optional message that will be printed that describes the failure. If omitted, a default will be used. | `None` | 321*bcb5dc79SHONG Yifan 322*bcb5dc79SHONG Yifan 323*bcb5dc79SHONG Yifan<a id="asserts.set_equals"></a> 324*bcb5dc79SHONG Yifan 325*bcb5dc79SHONG Yifan## asserts.set_equals 326*bcb5dc79SHONG Yifan 327*bcb5dc79SHONG Yifan<pre> 328*bcb5dc79SHONG Yifanasserts.set_equals(<a href="#asserts.set_equals-env">env</a>, <a href="#asserts.set_equals-expected">expected</a>, <a href="#asserts.set_equals-actual">actual</a>, <a href="#asserts.set_equals-msg">msg</a>) 329*bcb5dc79SHONG Yifan</pre> 330*bcb5dc79SHONG Yifan 331*bcb5dc79SHONG YifanAsserts that the given `expected` and `actual` sets are equal. 332*bcb5dc79SHONG Yifan 333*bcb5dc79SHONG Yifan**PARAMETERS** 334*bcb5dc79SHONG Yifan 335*bcb5dc79SHONG Yifan 336*bcb5dc79SHONG Yifan| Name | Description | Default Value | 337*bcb5dc79SHONG Yifan| :------------- | :------------- | :------------- | 338*bcb5dc79SHONG Yifan| <a id="asserts.set_equals-env"></a>env | The test environment returned by `unittest.begin`. | none | 339*bcb5dc79SHONG Yifan| <a id="asserts.set_equals-expected"></a>expected | The expected set resulting from some computation. | none | 340*bcb5dc79SHONG Yifan| <a id="asserts.set_equals-actual"></a>actual | The actual set returned by some computation. | none | 341*bcb5dc79SHONG Yifan| <a id="asserts.set_equals-msg"></a>msg | An optional message that will be printed that describes the failure. If omitted, a default will be used. | `None` | 342*bcb5dc79SHONG Yifan 343*bcb5dc79SHONG Yifan 344*bcb5dc79SHONG Yifan<a id="asserts.true"></a> 345*bcb5dc79SHONG Yifan 346*bcb5dc79SHONG Yifan## asserts.true 347*bcb5dc79SHONG Yifan 348*bcb5dc79SHONG Yifan<pre> 349*bcb5dc79SHONG Yifanasserts.true(<a href="#asserts.true-env">env</a>, <a href="#asserts.true-condition">condition</a>, <a href="#asserts.true-msg">msg</a>) 350*bcb5dc79SHONG Yifan</pre> 351*bcb5dc79SHONG Yifan 352*bcb5dc79SHONG YifanAsserts that the given `condition` is true. 353*bcb5dc79SHONG Yifan 354*bcb5dc79SHONG Yifan**PARAMETERS** 355*bcb5dc79SHONG Yifan 356*bcb5dc79SHONG Yifan 357*bcb5dc79SHONG Yifan| Name | Description | Default Value | 358*bcb5dc79SHONG Yifan| :------------- | :------------- | :------------- | 359*bcb5dc79SHONG Yifan| <a id="asserts.true-env"></a>env | The test environment returned by `unittest.begin`. | none | 360*bcb5dc79SHONG Yifan| <a id="asserts.true-condition"></a>condition | A value that will be evaluated in a Boolean context. | none | 361*bcb5dc79SHONG Yifan| <a id="asserts.true-msg"></a>msg | An optional message that will be printed that describes the failure. If omitted, a default will be used. | `"Expected condition to be true, but was false."` | 362*bcb5dc79SHONG Yifan 363*bcb5dc79SHONG Yifan 364*bcb5dc79SHONG Yifan<a id="loadingtest.equals"></a> 365*bcb5dc79SHONG Yifan 366*bcb5dc79SHONG Yifan## loadingtest.equals 367*bcb5dc79SHONG Yifan 368*bcb5dc79SHONG Yifan<pre> 369*bcb5dc79SHONG Yifanloadingtest.equals(<a href="#loadingtest.equals-env">env</a>, <a href="#loadingtest.equals-test_case">test_case</a>, <a href="#loadingtest.equals-expected">expected</a>, <a href="#loadingtest.equals-actual">actual</a>) 370*bcb5dc79SHONG Yifan</pre> 371*bcb5dc79SHONG Yifan 372*bcb5dc79SHONG YifanCreates a test case for asserting state at LOADING phase. 373*bcb5dc79SHONG Yifan 374*bcb5dc79SHONG Yifan**PARAMETERS** 375*bcb5dc79SHONG Yifan 376*bcb5dc79SHONG Yifan 377*bcb5dc79SHONG Yifan| Name | Description | Default Value | 378*bcb5dc79SHONG Yifan| :------------- | :------------- | :------------- | 379*bcb5dc79SHONG Yifan| <a id="loadingtest.equals-env"></a>env | Loading test env created from loadingtest.make | none | 380*bcb5dc79SHONG Yifan| <a id="loadingtest.equals-test_case"></a>test_case | Name of the test case | none | 381*bcb5dc79SHONG Yifan| <a id="loadingtest.equals-expected"></a>expected | Expected value to test | none | 382*bcb5dc79SHONG Yifan| <a id="loadingtest.equals-actual"></a>actual | Actual value received. | none | 383*bcb5dc79SHONG Yifan 384*bcb5dc79SHONG Yifan**RETURNS** 385*bcb5dc79SHONG Yifan 386*bcb5dc79SHONG YifanNone, creates test case 387*bcb5dc79SHONG Yifan 388*bcb5dc79SHONG Yifan 389*bcb5dc79SHONG Yifan<a id="loadingtest.make"></a> 390*bcb5dc79SHONG Yifan 391*bcb5dc79SHONG Yifan## loadingtest.make 392*bcb5dc79SHONG Yifan 393*bcb5dc79SHONG Yifan<pre> 394*bcb5dc79SHONG Yifanloadingtest.make(<a href="#loadingtest.make-name">name</a>) 395*bcb5dc79SHONG Yifan</pre> 396*bcb5dc79SHONG Yifan 397*bcb5dc79SHONG YifanCreates a loading phase test environment and test_suite. 398*bcb5dc79SHONG Yifan 399*bcb5dc79SHONG Yifan**PARAMETERS** 400*bcb5dc79SHONG Yifan 401*bcb5dc79SHONG Yifan 402*bcb5dc79SHONG Yifan| Name | Description | Default Value | 403*bcb5dc79SHONG Yifan| :------------- | :------------- | :------------- | 404*bcb5dc79SHONG Yifan| <a id="loadingtest.make-name"></a>name | name of the suite of tests to create | none | 405*bcb5dc79SHONG Yifan 406*bcb5dc79SHONG Yifan**RETURNS** 407*bcb5dc79SHONG Yifan 408*bcb5dc79SHONG Yifanloading phase environment passed to other loadingtest functions 409*bcb5dc79SHONG Yifan 410*bcb5dc79SHONG Yifan 411*bcb5dc79SHONG Yifan<a id="register_unittest_toolchains"></a> 412*bcb5dc79SHONG Yifan 413*bcb5dc79SHONG Yifan## register_unittest_toolchains 414*bcb5dc79SHONG Yifan 415*bcb5dc79SHONG Yifan<pre> 416*bcb5dc79SHONG Yifanregister_unittest_toolchains() 417*bcb5dc79SHONG Yifan</pre> 418*bcb5dc79SHONG Yifan 419*bcb5dc79SHONG YifanRegisters the toolchains for unittest users. 420*bcb5dc79SHONG Yifan 421*bcb5dc79SHONG Yifan 422*bcb5dc79SHONG Yifan 423*bcb5dc79SHONG Yifan<a id="unittest.begin"></a> 424*bcb5dc79SHONG Yifan 425*bcb5dc79SHONG Yifan## unittest.begin 426*bcb5dc79SHONG Yifan 427*bcb5dc79SHONG Yifan<pre> 428*bcb5dc79SHONG Yifanunittest.begin(<a href="#unittest.begin-ctx">ctx</a>) 429*bcb5dc79SHONG Yifan</pre> 430*bcb5dc79SHONG Yifan 431*bcb5dc79SHONG YifanBegins a unit test. 432*bcb5dc79SHONG Yifan 433*bcb5dc79SHONG YifanThis should be the first function called in a unit test implementation 434*bcb5dc79SHONG Yifanfunction. It initializes a "test environment" that is used to collect 435*bcb5dc79SHONG Yifanassertion failures so that they can be reported and logged at the end of the 436*bcb5dc79SHONG Yifantest. 437*bcb5dc79SHONG Yifan 438*bcb5dc79SHONG Yifan 439*bcb5dc79SHONG Yifan**PARAMETERS** 440*bcb5dc79SHONG Yifan 441*bcb5dc79SHONG Yifan 442*bcb5dc79SHONG Yifan| Name | Description | Default Value | 443*bcb5dc79SHONG Yifan| :------------- | :------------- | :------------- | 444*bcb5dc79SHONG Yifan| <a id="unittest.begin-ctx"></a>ctx | The Starlark context. Pass the implementation function's `ctx` argument in verbatim. | none | 445*bcb5dc79SHONG Yifan 446*bcb5dc79SHONG Yifan**RETURNS** 447*bcb5dc79SHONG Yifan 448*bcb5dc79SHONG YifanA test environment struct that must be passed to assertions and finally to 449*bcb5dc79SHONG Yifan`unittest.end`. Do not rely on internal details about the fields in this 450*bcb5dc79SHONG Yifanstruct as it may change. 451*bcb5dc79SHONG Yifan 452*bcb5dc79SHONG Yifan 453*bcb5dc79SHONG Yifan<a id="unittest.end"></a> 454*bcb5dc79SHONG Yifan 455*bcb5dc79SHONG Yifan## unittest.end 456*bcb5dc79SHONG Yifan 457*bcb5dc79SHONG Yifan<pre> 458*bcb5dc79SHONG Yifanunittest.end(<a href="#unittest.end-env">env</a>) 459*bcb5dc79SHONG Yifan</pre> 460*bcb5dc79SHONG Yifan 461*bcb5dc79SHONG YifanEnds a unit test and logs the results. 462*bcb5dc79SHONG Yifan 463*bcb5dc79SHONG YifanThis must be called and returned at the end of a unit test implementation function so 464*bcb5dc79SHONG Yifanthat the results are reported. 465*bcb5dc79SHONG Yifan 466*bcb5dc79SHONG Yifan 467*bcb5dc79SHONG Yifan**PARAMETERS** 468*bcb5dc79SHONG Yifan 469*bcb5dc79SHONG Yifan 470*bcb5dc79SHONG Yifan| Name | Description | Default Value | 471*bcb5dc79SHONG Yifan| :------------- | :------------- | :------------- | 472*bcb5dc79SHONG Yifan| <a id="unittest.end-env"></a>env | The test environment returned by `unittest.begin`. | none | 473*bcb5dc79SHONG Yifan 474*bcb5dc79SHONG Yifan**RETURNS** 475*bcb5dc79SHONG Yifan 476*bcb5dc79SHONG YifanA list of providers needed to automatically register the test result. 477*bcb5dc79SHONG Yifan 478*bcb5dc79SHONG Yifan 479*bcb5dc79SHONG Yifan<a id="unittest.fail"></a> 480*bcb5dc79SHONG Yifan 481*bcb5dc79SHONG Yifan## unittest.fail 482*bcb5dc79SHONG Yifan 483*bcb5dc79SHONG Yifan<pre> 484*bcb5dc79SHONG Yifanunittest.fail(<a href="#unittest.fail-env">env</a>, <a href="#unittest.fail-msg">msg</a>) 485*bcb5dc79SHONG Yifan</pre> 486*bcb5dc79SHONG Yifan 487*bcb5dc79SHONG YifanUnconditionally causes the current test to fail. 488*bcb5dc79SHONG Yifan 489*bcb5dc79SHONG Yifan**PARAMETERS** 490*bcb5dc79SHONG Yifan 491*bcb5dc79SHONG Yifan 492*bcb5dc79SHONG Yifan| Name | Description | Default Value | 493*bcb5dc79SHONG Yifan| :------------- | :------------- | :------------- | 494*bcb5dc79SHONG Yifan| <a id="unittest.fail-env"></a>env | The test environment returned by `unittest.begin`. | none | 495*bcb5dc79SHONG Yifan| <a id="unittest.fail-msg"></a>msg | The message to log describing the failure. | none | 496*bcb5dc79SHONG Yifan 497*bcb5dc79SHONG Yifan 498*bcb5dc79SHONG Yifan<a id="unittest.make"></a> 499*bcb5dc79SHONG Yifan 500*bcb5dc79SHONG Yifan## unittest.make 501*bcb5dc79SHONG Yifan 502*bcb5dc79SHONG Yifan<pre> 503*bcb5dc79SHONG Yifanunittest.make(<a href="#unittest.make-impl">impl</a>, <a href="#unittest.make-attrs">attrs</a>, <a href="#unittest.make-doc">doc</a>, <a href="#unittest.make-toolchains">toolchains</a>) 504*bcb5dc79SHONG Yifan</pre> 505*bcb5dc79SHONG Yifan 506*bcb5dc79SHONG YifanCreates a unit test rule from its implementation function. 507*bcb5dc79SHONG Yifan 508*bcb5dc79SHONG YifanEach unit test is defined in an implementation function that must then be 509*bcb5dc79SHONG Yifanassociated with a rule so that a target can be built. This function handles 510*bcb5dc79SHONG Yifanthe boilerplate to create and return a test rule and captures the 511*bcb5dc79SHONG Yifanimplementation function's name so that it can be printed in test feedback. 512*bcb5dc79SHONG Yifan 513*bcb5dc79SHONG YifanThe optional `attrs` argument can be used to define dependencies for this 514*bcb5dc79SHONG Yifantest, in order to form unit tests of rules. 515*bcb5dc79SHONG Yifan 516*bcb5dc79SHONG YifanThe optional `toolchains` argument can be used to define toolchain 517*bcb5dc79SHONG Yifandependencies for this test. 518*bcb5dc79SHONG Yifan 519*bcb5dc79SHONG YifanAn example of a unit test: 520*bcb5dc79SHONG Yifan 521*bcb5dc79SHONG Yifan``` 522*bcb5dc79SHONG Yifandef _your_test(ctx): 523*bcb5dc79SHONG Yifan env = unittest.begin(ctx) 524*bcb5dc79SHONG Yifan 525*bcb5dc79SHONG Yifan # Assert statements go here 526*bcb5dc79SHONG Yifan 527*bcb5dc79SHONG Yifan return unittest.end(env) 528*bcb5dc79SHONG Yifan 529*bcb5dc79SHONG Yifanyour_test = unittest.make(_your_test) 530*bcb5dc79SHONG Yifan``` 531*bcb5dc79SHONG Yifan 532*bcb5dc79SHONG YifanRecall that names of test rules must end in `_test`. 533*bcb5dc79SHONG Yifan 534*bcb5dc79SHONG Yifan 535*bcb5dc79SHONG Yifan**PARAMETERS** 536*bcb5dc79SHONG Yifan 537*bcb5dc79SHONG Yifan 538*bcb5dc79SHONG Yifan| Name | Description | Default Value | 539*bcb5dc79SHONG Yifan| :------------- | :------------- | :------------- | 540*bcb5dc79SHONG Yifan| <a id="unittest.make-impl"></a>impl | The implementation function of the unit test. | none | 541*bcb5dc79SHONG Yifan| <a id="unittest.make-attrs"></a>attrs | An optional dictionary to supplement the attrs passed to the unit test's `rule()` constructor. | `{}` | 542*bcb5dc79SHONG Yifan| <a id="unittest.make-doc"></a>doc | A description of the rule that can be extracted by documentation generating tools. | `""` | 543*bcb5dc79SHONG Yifan| <a id="unittest.make-toolchains"></a>toolchains | An optional list to supplement the toolchains passed to the unit test's `rule()` constructor. | `[]` | 544*bcb5dc79SHONG Yifan 545*bcb5dc79SHONG Yifan**RETURNS** 546*bcb5dc79SHONG Yifan 547*bcb5dc79SHONG YifanA rule definition that should be stored in a global whose name ends in 548*bcb5dc79SHONG Yifan`_test`. 549*bcb5dc79SHONG Yifan 550*bcb5dc79SHONG Yifan 551*bcb5dc79SHONG Yifan<a id="unittest.suite"></a> 552*bcb5dc79SHONG Yifan 553*bcb5dc79SHONG Yifan## unittest.suite 554*bcb5dc79SHONG Yifan 555*bcb5dc79SHONG Yifan<pre> 556*bcb5dc79SHONG Yifanunittest.suite(<a href="#unittest.suite-name">name</a>, <a href="#unittest.suite-test_rules">test_rules</a>) 557*bcb5dc79SHONG Yifan</pre> 558*bcb5dc79SHONG Yifan 559*bcb5dc79SHONG YifanDefines a `test_suite` target that contains multiple tests. 560*bcb5dc79SHONG Yifan 561*bcb5dc79SHONG YifanAfter defining your test rules in a `.bzl` file, you need to create targets 562*bcb5dc79SHONG Yifanfrom those rules so that `blaze test` can execute them. Doing this manually 563*bcb5dc79SHONG Yifanin a BUILD file would consist of listing each test in your `load` statement 564*bcb5dc79SHONG Yifanand then creating each target one by one. To reduce duplication, we recommend 565*bcb5dc79SHONG Yifanwriting a macro in your `.bzl` file to instantiate all targets, and calling 566*bcb5dc79SHONG Yifanthat macro from your BUILD file so you only have to load one symbol. 567*bcb5dc79SHONG Yifan 568*bcb5dc79SHONG YifanYou can use this function to create the targets and wrap them in a single 569*bcb5dc79SHONG Yifantest_suite target. If a test rule requires no arguments, you can simply list 570*bcb5dc79SHONG Yifanit as an argument. If you wish to supply attributes explicitly, you can do so 571*bcb5dc79SHONG Yifanusing `partial.make()`. For instance, in your `.bzl` file, you could write: 572*bcb5dc79SHONG Yifan 573*bcb5dc79SHONG Yifan``` 574*bcb5dc79SHONG Yifandef your_test_suite(): 575*bcb5dc79SHONG Yifan unittest.suite( 576*bcb5dc79SHONG Yifan "your_test_suite", 577*bcb5dc79SHONG Yifan your_test, 578*bcb5dc79SHONG Yifan your_other_test, 579*bcb5dc79SHONG Yifan partial.make(yet_another_test, timeout = "short"), 580*bcb5dc79SHONG Yifan ) 581*bcb5dc79SHONG Yifan``` 582*bcb5dc79SHONG Yifan 583*bcb5dc79SHONG YifanThen, in your `BUILD` file, simply load the macro and invoke it to have all 584*bcb5dc79SHONG Yifanof the targets created: 585*bcb5dc79SHONG Yifan 586*bcb5dc79SHONG Yifan``` 587*bcb5dc79SHONG Yifanload("//path/to/your/package:tests.bzl", "your_test_suite") 588*bcb5dc79SHONG Yifanyour_test_suite() 589*bcb5dc79SHONG Yifan``` 590*bcb5dc79SHONG Yifan 591*bcb5dc79SHONG YifanIf you pass _N_ unit test rules to `unittest.suite`, _N_ + 1 targets will be 592*bcb5dc79SHONG Yifancreated: a `test_suite` target named `${name}` (where `${name}` is the name 593*bcb5dc79SHONG Yifanargument passed in here) and targets named `${name}_test_${i}`, where `${i}` 594*bcb5dc79SHONG Yifanis the index of the test in the `test_rules` list, which is used to uniquely 595*bcb5dc79SHONG Yifanname each target. 596*bcb5dc79SHONG Yifan 597*bcb5dc79SHONG Yifan 598*bcb5dc79SHONG Yifan**PARAMETERS** 599*bcb5dc79SHONG Yifan 600*bcb5dc79SHONG Yifan 601*bcb5dc79SHONG Yifan| Name | Description | Default Value | 602*bcb5dc79SHONG Yifan| :------------- | :------------- | :------------- | 603*bcb5dc79SHONG Yifan| <a id="unittest.suite-name"></a>name | The name of the `test_suite` target, and the prefix of all the test target names. | none | 604*bcb5dc79SHONG Yifan| <a id="unittest.suite-test_rules"></a>test_rules | A list of test rules defines by `unittest.test`. | none | 605*bcb5dc79SHONG Yifan 606*bcb5dc79SHONG Yifan 607