การทดสอบ

รายงานปัญหา ดูแหล่งที่มา Nightly · 8.4 · 8.3 · 8.2 · 8.1 · 8.0 · 7.6

การทดสอบโค้ด 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

นอกเหนือจากคำสั่ง 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)

  • ป้ายกำกับของเป้าหมายของกฎภายใต้การทดสอบและการขึ้นต่อกันของกฎเหล่านั้น ควรมีคำนำหน้าเป็น 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)

นอกจากนี้ โปรดตรวจสอบว่าเป้าหมายภายใต้การทดสอบติดแท็กเป็น "ด้วยตนเอง" โดยเฉพาะ หากไม่มีการดำเนินการนี้ การสร้างเป้าหมายทั้งหมดในแพ็กเกจโดยใช้ :all จะส่งผลให้ สร้างเป้าหมายที่ตั้งใจให้ล้มเหลวและจะแสดงข้อผิดพลาดในการสร้าง เมื่อใช้ "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 อาร์ติแฟกต์ที่ต้องการตรวจสอบควรเป็นทรัพยากร Dependency ของเป้าหมายนี้ หากตรรกะการตรวจสอบใช้ซ้ำได้สำหรับการทดสอบหลายรายการ ควรเป็นสคริปต์ที่รับอาร์กิวเมนต์บรรทัดคำสั่งซึ่งควบคุมโดยแอตทริบิวต์ 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"],
)

การใช้กฎที่กำหนดเอง

อีกทางเลือกที่ซับซ้อนกว่าคือการเขียนสคริปต์เชลล์เป็นเทมเพลตที่ กฎใหม่จะสร้างอินสแตนซ์ ซึ่งเกี่ยวข้องกับการเปลี่ยนเส้นทางและตรรกะ 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

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