การทดสอบโค้ด Starlark ใน Bazel ทำได้หลายวิธี หน้านี้รวบรวมแนวทางปฏิบัติที่ดีที่สุดและเฟรมเวิร์กปัจจุบันตามกรณีการใช้งาน
กฎการทดสอบ
Skylib มีเฟรมเวิร์กการทดสอบชื่อ
unittest.bzl
สำหรับตรวจสอบลักษณะการทำงานของกฎในเวลาวิเคราะห์ เช่น การดำเนินการและ
ผู้ให้บริการ การทดสอบดังกล่าวเรียกว่า "การทดสอบการวิเคราะห์" และปัจจุบันเป็นตัวเลือกที่ดีที่สุดสำหรับการทดสอบการทำงานภายในของกฎ
ข้อควรระวังบางประการ
การยืนยันการทดสอบเกิดขึ้นภายในบิลด์ ไม่ใช่กระบวนการตัวเรียกใช้การทดสอบแยกต่างหาก เป้าหมายที่สร้างขึ้นโดยการทดสอบต้องมีชื่อที่ไม่ขัดแย้งกับเป้าหมายจากการทดสอบอื่นๆ หรือจากบิลด์ Bazel จะมองว่าข้อผิดพลาดที่เกิดขึ้นระหว่างการทดสอบเป็นการหยุดชะงักของบิลด์ ไม่ใช่การทดสอบล้มเหลว
ต้องใช้โค้ดจำนวนมากเพื่อตั้งค่ากฎที่อยู่ระหว่างการทดสอบและกฎที่มีการยืนยันการทดสอบ โค้ดนี้อาจดูน่ากลัวในตอนแรก โปรดทราบว่าระบบจะประเมินมาโคร และสร้างเป้าหมายในระยะการโหลด ขณะที่ฟังก์ชันการติดตั้งใช้งานกฎ จะไม่ทำงานจนกว่าจะถึงระยะการวิเคราะห์
การทดสอบการวิเคราะห์มีไว้เพื่อให้มีขนาดเล็กและน้ำหนักเบา ฟีเจอร์บางอย่างของเฟรมเวิร์กการทดสอบการวิเคราะห์ถูกจำกัดไว้สำหรับการยืนยันเป้าหมายที่มีการขึ้นต่อกันแบบทรานซิทีฟสูงสุด (ปัจจุบันคือ 500 รายการ) เนื่องจากฟีเจอร์เหล่านี้ส่งผลต่อประสิทธิภาพเมื่อใช้กับการทดสอบขนาดใหญ่
หลักการพื้นฐานคือการกำหนดกฎการทดสอบที่ขึ้นอยู่กับกฎที่อยู่ระหว่างการทดสอบ ซึ่งจะทำให้กฎการทดสอบเข้าถึงผู้ให้บริการของกฎที่อยู่ระหว่างการทดสอบได้
ฟังก์ชันการติดตั้งใช้งานของกฎการทดสอบจะทำการยืนยัน หากเกิดข้อผิดพลาด ระบบจะไม่แสดงข้อผิดพลาดเหล่านั้นทันทีโดยการเรียกใช้ fail() (ซึ่งจะทริกเกอร์ข้อผิดพลาดของบิลด์ในเวลาวิเคราะห์) แต่จะจัดเก็บข้อผิดพลาดไว้ในสคริปต์ที่สร้างขึ้นซึ่งล้มเหลวในเวลาการดำเนินการทดสอบแทน
ดูตัวอย่างของเล่นขนาดเล็กด้านล่าง ตามด้วยตัวอย่างที่ตรวจสอบการดำเนินการ
ตัวอย่างเล็กๆ
//mypkg/myrules.bzl:
MyInfo = provider(fields = {
"val": "string value",
"out": "output File",
})
def _myrule_impl(ctx):
"""Rule that just generates a file and returns a provider."""
out = ctx.actions.declare_file(ctx.label.name + ".out")
ctx.actions.write(out, "abc")
return [MyInfo(val="some value", out=out)]
myrule = rule(
implementation = _myrule_impl,
)
//mypkg/myrules_test.bzl:
load("@bazel_skylib//lib:unittest.bzl", "asserts", "analysistest")
load(":myrules.bzl", "myrule", "MyInfo")
# ==== Check the provider contents ====
def _provider_contents_test_impl(ctx):
env = analysistest.begin(ctx)
target_under_test = analysistest.target_under_test(env)
# If preferred, could pass these values as "expected" and "actual" keyword
# arguments.
asserts.equals(env, "some value", target_under_test[MyInfo].val)
# If you forget to return end(), you will get an error about an analysis
# test needing to return an instance of AnalysisTestResultInfo.
return analysistest.end(env)
# Create the testing rule to wrap the test logic. This must be bound to a global
# variable, not called in a macro's body, since macros get evaluated at loading
# time but the rule gets evaluated later, at analysis time. Since this is a test
# rule, its name must end with "_test".
provider_contents_test = analysistest.make(_provider_contents_test_impl)
# Macro to setup the test.
def _test_provider_contents():
# Rule under test. Be sure to tag 'manual', as this target should not be
# built using `:all` except as a dependency of the test.
myrule(name = "provider_contents_subject", tags = ["manual"])
# Testing rule.
provider_contents_test(name = "provider_contents_test",
target_under_test = ":provider_contents_subject")
# Note the target_under_test attribute is how the test rule depends on
# the real rule target.
# Entry point from the BUILD file; macro for running each test case's macro and
# declaring a test suite that wraps them together.
def myrules_test_suite(name):
# Call all test functions and wrap their targets in a suite.
_test_provider_contents()
# ...
native.test_suite(
name = name,
tests = [
":provider_contents_test",
# ...
],
)
//mypkg/BUILD:
load(":myrules.bzl", "myrule")
load(":myrules_test.bzl", "myrules_test_suite")
# Production use of the rule.
myrule(
name = "mytarget",
)
# Call a macro that defines targets that perform the tests at analysis time,
# and that can be executed with "bazel test" to return the result.
myrules_test_suite(name = "myrules_test")
คุณเรียกใช้การทดสอบได้ด้วย bazel test //mypkg:myrules_test
ไฟล์นี้มี 2 ส่วนหลักนอกเหนือจากคำสั่ง load() เริ่มต้น
การทดสอบเอง ซึ่งแต่ละรายการประกอบด้วย 1) ฟังก์ชันการติดตั้งใช้งานในเวลาวิเคราะห์สำหรับกฎการทดสอบ 2) การประกาศกฎการทดสอบผ่าน
analysistest.make()และ 3) ฟังก์ชันในเวลาโหลด (มาโคร) สำหรับประกาศกฎที่อยู่ระหว่างการทดสอบ (และการขึ้นต่อกัน) และกฎการทดสอบ หากการยืนยันไม่เปลี่ยนแปลงระหว่างกรณีทดสอบ 1) และ 2) อาจใช้ร่วมกันได้ในหลายกรณีทดสอบฟังก์ชันชุดการทดสอบ ซึ่งเรียกใช้ฟังก์ชันในเวลาโหลดสำหรับการทดสอบแต่ละรายการ และประกาศเป้าหมาย
test_suiteที่รวมการทดสอบทั้งหมดเข้าด้วยกัน
ทำตามรูปแบบการตั้งชื่อที่แนะนำเพื่อให้สอดคล้องกัน โดยให้ foo แทนส่วนของชื่อการทดสอบที่อธิบายสิ่งที่การทดสอบกำลังตรวจสอบ (provider_contents ในตัวอย่างด้านบน) ตัวอย่างเช่น เมธอดการทดสอบ JUnit จะมีชื่อว่า testFoo
จากนั้นให้ทำดังนี้
มาโครที่สร้างการทดสอบและเป้าหมายที่อยู่ระหว่างการทดสอบควรมีชื่อว่า
_test_foo(_test_provider_contents)ประเภทกฎการทดสอบควรมีชื่อว่า
foo_test(provider_contents_test)ป้ายกำกับของเป้าหมายของกฎประเภทนี้ควรเป็น
foo_test(provider_contents_test)ฟังก์ชันการติดตั้งใช้งานสำหรับกฎการทดสอบควรมีชื่อว่า
_foo_test_impl(_provider_contents_test_impl)ป้ายกำกับของเป้าหมายของกฎที่อยู่ระหว่างการทดสอบและการขึ้นต่อกันควรมีคำนำหน้าเป็น
foo_(provider_contents_)
โปรดทราบว่าป้ายกำกับของเป้าหมายทั้งหมดอาจขัดแย้งกับป้ายกำกับอื่นๆ ในแพ็กเกจ BUILD เดียวกัน ดังนั้นการใช้ชื่อที่ไม่ซ้ำกันสำหรับการทดสอบจึงเป็นประโยชน์
การทดสอบความล้มเหลว
การยืนยันว่ากฎล้มเหลวเมื่อได้รับอินพุตบางอย่างหรืออยู่ในสถานะหนึ่งๆ อาจมีประโยชน์ คุณทำได้โดยใช้เฟรมเวิร์กการทดสอบการวิเคราะห์ดังนี้
กฎการทดสอบที่สร้างด้วย analysistest.make ควรระบุ expect_failure ดังนี้
failure_testing_test = analysistest.make(
_failure_testing_test_impl,
expect_failure = True,
)
การติดตั้งใช้งานกฎการทดสอบควรทำการยืนยันลักษณะของความล้มเหลวที่เกิดขึ้น (โดยเฉพาะข้อความแสดงข้อผิดพลาด)
def _failure_testing_test_impl(ctx):
env = analysistest.begin(ctx)
asserts.expect_failure(env, "This rule should never work")
return analysistest.end(env)
และตรวจสอบว่าเป้าหมายที่อยู่ระหว่างการทดสอบมีแท็ก "manual" โดยเฉพาะ
หากไม่มีแท็กนี้ การสร้างเป้าหมายทั้งหมดในแพ็กเกจโดยใช้ :all จะส่งผลให้มีการสร้างเป้าหมายที่ตั้งใจให้ล้มเหลวและแสดงข้อผิดพลาดของบิลด์ เมื่อใช้ 'manual' ระบบจะสร้างเป้าหมายที่อยู่ระหว่างการทดสอบก็ต่อเมื่อมีการระบุอย่างชัดเจน หรือเป็นทรัพยากร Dependency ของเป้าหมายที่ไม่ใช่ 'manual' (เช่น กฎการทดสอบ)
def _test_failure():
myrule(name = "this_should_fail", tags = ["manual"])
failure_testing_test(name = "failure_testing_test",
target_under_test = ":this_should_fail")
# Then call _test_failure() in the macro which generates the test suite and add
# ":failure_testing_test" to the suite's test targets.
การยืนยันการดำเนินการที่ลงทะเบียน
คุณอาจต้องการเขียนการทดสอบที่ทำการยืนยันเกี่ยวกับการดำเนินการที่กฎลงทะเบียนไว้ เช่น การใช้ ctx.actions.run() คุณทำได้ในฟังก์ชันการติดตั้งใช้งานกฎการทดสอบการวิเคราะห์ ตัวอย่าง
def _inspect_actions_test_impl(ctx):
env = analysistest.begin(ctx)
target_under_test = analysistest.target_under_test(env)
actions = analysistest.target_actions(env)
asserts.equals(env, 1, len(actions))
action_output = actions[0].outputs.to_list()[0]
asserts.equals(
env, target_under_test.label.name + ".out", action_output.basename)
return analysistest.end(env)
โปรดทราบว่า analysistest.target_actions(env) จะแสดงผลรายการ
Actionออบเจ็กต์ ซึ่งแสดงถึงการดำเนินการที่
เป้าหมายที่อยู่ระหว่างการทดสอบลงทะเบียนไว้
การยืนยันลักษณะการทำงานของกฎภายใต้แฟล็กต่างๆ
คุณอาจต้องการยืนยันว่ากฎจริงมีลักษณะการทำงานบางอย่างเมื่อได้รับแฟล็กบิลด์บางอย่าง ตัวอย่างเช่น กฎอาจมีลักษณะการทำงานแตกต่างกันหากผู้ใช้ระบุ
bazel build //mypkg:real_target -c opt
ปะทะกับ
bazel build //mypkg:real_target -c dbg
เมื่อมองแวบแรก คุณอาจทำได้โดยการทดสอบเป้าหมายที่อยู่ระหว่างการทดสอบโดยใช้แฟล็กบิลด์ที่ต้องการ
bazel test //mypkg:myrules_test -c opt
แต่จากนั้นชุดการทดสอบจะไม่มีการทดสอบที่ยืนยันลักษณะการทำงานของกฎภายใต้ -c opt และการทดสอบอื่นที่ยืนยันลักษณะการทำงานของกฎภายใต้ -c dbg พร้อมกัน การทดสอบทั้ง 2 รายการจะไม่สามารถทำงานในบิลด์เดียวกันได้
คุณแก้ปัญหานี้ได้โดยการระบุแฟล็กบิลด์ที่ต้องการเมื่อกำหนดกฎการทดสอบ
myrule_c_opt_test = analysistest.make(
_myrule_c_opt_test_impl,
config_settings = {
"//command_line_option:compilation_mode": "opt",
},
)
โดยปกติแล้ว ระบบจะวิเคราะห์เป้าหมายที่อยู่ระหว่างการทดสอบโดยใช้แฟล็กบิลด์ปัจจุบัน
การระบุ config_settings จะลบล้างค่าของตัวเลือกบรรทัดคำสั่งที่ระบุ (ตัวเลือกที่ไม่ได้ระบุจะยังคงค่าจากบรรทัดคำสั่งจริง)
ในพจนานุกรม config_settings ที่ระบุ แฟล็กบรรทัดคำสั่งต้องมีคำนำหน้าเป็นค่าตัวยึดตำแหน่งพิเศษ //command_line_option: ดังที่แสดงด้านบน
การตรวจสอบอาร์ติแฟกต์
วิธีหลักๆ ในการตรวจสอบว่าไฟล์ที่สร้างขึ้นถูกต้องมีดังนี้
คุณสามารถเขียนสคริปต์การทดสอบใน Shell, Python หรือภาษาอื่น และสร้างเป้าหมายของกฎประเภท
*_testที่เหมาะสมคุณสามารถใช้กฎเฉพาะสำหรับการทดสอบประเภทที่ต้องการดำเนินการ
การใช้เป้าหมายการทดสอบ
วิธีที่ตรงไปตรงมาที่สุดในการตรวจสอบอาร์ติแฟกต์คือการเขียนสคริปต์และเพิ่มเป้าหมาย *_test ลงในไฟล์ BUILD อาร์ติแฟกต์เฉพาะที่ต้องการตรวจสอบควรเป็นการขึ้นต่อกันของข้อมูลของเป้าหมายนี้ หากตรรกะการตรวจสอบใช้ซ้ำได้สำหรับการทดสอบหลายรายการ ควรเป็นสคริปต์ที่รับอาร์กิวเมนต์บรรทัดคำสั่งซึ่งควบคุมโดยแอตทริบิวต์ args ของเป้าหมายการทดสอบ นี่คือตัวอย่างที่ตรวจสอบว่าเอาต์พุตของ myrule จากด้านบนคือ "abc"
//mypkg/myrule_validator.sh:
if [ "$(cat $1)" = "abc" ]; then
echo "Passed"
exit 0
else
echo "Failed"
exit 1
fi
//mypkg/BUILD:
...
myrule(
name = "mytarget",
)
...
# Needed for each target whose artifacts are to be checked.
sh_test(
name = "validate_mytarget",
srcs = [":myrule_validator.sh"],
args = ["$(location :mytarget.out)"],
data = [":mytarget.out"],
)
การใช้กฎที่กำหนดเอง
อีกทางเลือกที่ซับซ้อนกว่าคือการเขียนสคริปต์ของ Shell เป็นเทมเพลตที่กฎใหม่สร้างขึ้น ซึ่งเกี่ยวข้องกับตรรกะ Starlark และการอ้างอิงทางอ้อมมากขึ้น แต่จะทำให้ไฟล์ BUILD สะอาดขึ้น ข้อดีเพิ่มเติมคือคุณสามารถประมวลผลอาร์กิวเมนต์ล่วงหน้าใน Starlark แทนที่จะทำในสคริปต์ และสคริปต์จะอธิบายตัวเองได้มากขึ้นเล็กน้อยเนื่องจากใช้ตัวยึดตำแหน่งสัญลักษณ์ (สำหรับการแทนที่) แทนตัวเลข (สำหรับอาร์กิวเมนต์)
//mypkg/myrule_validator.sh.template:
if [ "$(cat %TARGET%)" = "abc" ]; then
echo "Passed"
exit 0
else
echo "Failed"
exit 1
fi
//mypkg/myrule_validation.bzl:
def _myrule_validation_test_impl(ctx):
"""Rule for instantiating myrule_validator.sh.template for a given target."""
exe = ctx.outputs.executable
target = ctx.file.target
ctx.actions.expand_template(output = exe,
template = ctx.file._script,
is_executable = True,
substitutions = {
"%TARGET%": target.short_path,
})
# This is needed to make sure the output file of myrule is visible to the
# resulting instantiated script.
return [DefaultInfo(runfiles=ctx.runfiles(files=[target]))]
myrule_validation_test = rule(
implementation = _myrule_validation_test_impl,
attrs = {"target": attr.label(allow_single_file=True),
# You need an implicit dependency in order to access the template.
# A target could potentially override this attribute to modify
# the test logic.
"_script": attr.label(allow_single_file=True,
default=Label("//mypkg:myrule_validator"))},
test = True,
)
//mypkg/BUILD:
...
myrule(
name = "mytarget",
)
...
# Needed just once, to expose the template. Could have also used export_files(),
# and made the _script attribute set allow_files=True.
filegroup(
name = "myrule_validator",
srcs = [":myrule_validator.sh.template"],
)
# Needed for each target whose artifacts are to be checked. Notice that you no
# longer have to specify the output file name in a data attribute, or its
# $(location) expansion in an args attribute, or the label for the script
# (unless you want to override it).
myrule_validation_test(
name = "validate_mytarget",
target = ":mytarget",
)
หรือคุณจะฝังเทมเพลตลงในไฟล์ .bzl เป็นสตริงและขยายเทมเพลตในระยะการวิเคราะห์โดยใช้เมธอด str.format หรือการจัดรูปแบบ % แทนการใช้การดำเนินการขยายเทมเพลตก็ได้
การทดสอบยูทิลิตี Starlark
คุณสามารถใช้เฟรมเวิร์ก Skylib's
unittest.bzl เพื่อทดสอบฟังก์ชันยูทิลิตี (นั่นคือ ฟังก์ชันที่ไม่ใช่มาโครหรือการติดตั้งใช้งานกฎ) คุณสามารถใช้ unittest แทนไลบรารีของ unittest.bzl's
analysistest สำหรับชุดการทดสอบดังกล่าว คุณสามารถใช้ฟังก์ชันความสะดวก unittest.suite() เพื่อลดโค้ด
//mypkg/myhelpers.bzl:
def myhelper():
return "abc"
//mypkg/myhelpers_test.bzl:
load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest")
load(":myhelpers.bzl", "myhelper")
def _myhelper_test_impl(ctx):
env = unittest.begin(ctx)
asserts.equals(env, "abc", myhelper())
return unittest.end(env)
myhelper_test = unittest.make(_myhelper_test_impl)
# No need for a test_myhelper() setup function.
def myhelpers_test_suite(name):
# unittest.suite() takes care of instantiating the testing rules and creating
# a test_suite.
unittest.suite(
name,
myhelper_test,
# ...
)
//mypkg/BUILD:
load(":myhelpers_test.bzl", "myhelpers_test_suite")
myhelpers_test_suite(name = "myhelpers_tests")
ดูตัวอย่างเพิ่มเติมได้ที่การทดสอบของ Skylib เอง tests.