การทดสอบ

รายงานปัญหา ดูแหล่งที่มา

การทดสอบโค้ด Starlark ใน Bazel ทำได้หลายวิธี หน้านี้จะรวบรวมแนวทางปฏิบัติแนะนำและเฟรมเวิร์กในปัจจุบันตามกรณีการใช้งาน

กฎการทดสอบ

Skylib มีเฟรมเวิร์กการทดสอบชื่อ unittest.bzl สำหรับการตรวจสอบพฤติกรรมเวลาการวิเคราะห์ของกฎ เช่น การดำเนินการและผู้ให้บริการ การทดสอบดังกล่าวเรียกว่า "การทดสอบการวิเคราะห์" และในขณะนี้เป็นตัวเลือกที่ดีที่สุดสำหรับการทดสอบการทำงานภายในของกฎ

ข้อควรระวังบางประการมีดังนี้

  • การยืนยันการทดสอบจะเกิดขึ้นภายในบิลด์ ไม่ใช่กระบวนการของตัวดำเนินการทดสอบที่แยกต่างหาก เป้าหมายที่สร้างโดยการทดสอบจะต้องมีการตั้งชื่อไม่ให้ขัดแย้งกับเป้าหมายจากการทดสอบอื่นหรือจากบิลด์ Bazel มองว่าข้อผิดพลาดที่เกิดขึ้นระหว่างการทดสอบเป็นการหยุดทำงานของเวอร์ชัน ไม่ใช่ความล้มเหลวในการทดสอบ

  • คุณจำเป็นต้องใช้ต้นแบบที่เหมาะสมในการตั้งค่ากฎที่อยู่ระหว่างการทดสอบและกฎที่มีการยืนยันการทดสอบ ต้นแบบนี้อาจดูน่ากลัวในตอนแรก โปรดทราบว่ามาโครจะได้รับการประเมินและเป้าหมายที่สร้างขึ้นในระหว่างขั้นตอนการโหลด ขณะที่ฟังก์ชันการใช้งานกฎจะไม่ทำงานจนกระทั่งภายหลังในระหว่างช่วงการวิเคราะห์

  • การทดสอบการวิเคราะห์มีวัตถุประสงค์ให้มีจำนวนค่อนข้างเล็กและใช้ทรัพยากรน้อย ฟีเจอร์บางอย่างของกรอบการทดสอบการวิเคราะห์จำกัดไว้สำหรับการยืนยันเป้าหมายที่มีทรัพยากร Dependency แบบทรานซิชันสูงสุด (ปัจจุบันคือ 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

นอกเหนือจากคำสั่ง load() เบื้องต้นแล้ว ยังมี 2 ส่วนหลักในไฟล์ดังนี้

  • ตัวทดสอบจะประกอบไปด้วย 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)

  • ป้ายกำกับของเป้าหมายสำหรับกฎภายใต้การทดสอบและทรัพยากร Dependency ควรมี foo_ (provider_contents_) นำหน้า

โปรดทราบว่าป้ายกำกับของเป้าหมายทั้งหมดอาจขัดแย้งกับป้ายกำกับอื่นๆ ในแพ็กเกจ "สร้าง" เดียวกัน จึงควรใช้ชื่อที่ไม่ซ้ำกันสำหรับการทดสอบ

การทดสอบล้มเหลว

การตรวจสอบว่ากฎทำงานไม่สำเร็จเมื่อป้อนอินพุตบางอย่างหรืออยู่ในสถานะใดสถานะหนึ่งอาจเป็นประโยชน์ ซึ่งทำได้โดยใช้เฟรมเวิร์กการทดสอบการวิเคราะห์ ดังนี้

กฎทดสอบที่สร้างด้วย 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)

นอกจากนี้ ให้ตรวจสอบให้แน่ใจว่าเป้าหมายที่อยู่ในการทดสอบมีการติดแท็กเป็น "ด้วยตนเอง" โดยเฉพาะ หากไม่เป็นเช่นนั้น การสร้างเป้าหมายทั้งหมดในแพ็กเกจโดยใช้ :all จะส่งผลให้การสร้างเป้าหมายที่ล้มเหลวโดยเจตนาและจะแสดงความล้มเหลวของบิลด์ เมื่อใช้ "ด้วยตนเอง" เป้าหมายที่อยู่ระหว่างการทดสอบจะสร้างก็ต่อเมื่อมีการระบุอย่างชัดเจน หรือเป็นทรัพยากร Dependency ของเป้าหมายที่ไม่ใช่ด้วยตนเอง (เช่น กฎการทดสอบ) ดังนี้

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 unittest.bzl สามารถใช้เพื่อทดสอบฟังก์ชันยูทิลิตีได้ (ซึ่งก็คือฟังก์ชันที่ไม่ใช่มาโครหรือการใช้กฎ) ระบบอาจใช้ unittest แทนที่จะใช้ไลบรารี analysistest ของ unittest.bzl สำหรับชุดทดสอบดังกล่าว ฟังก์ชันความสะดวก unittest.suite() สามารถใช้เพื่อลด Boilerplate ได้

//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 เอง