การทดสอบ

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

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

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 พร้อมกันได้ การทดสอบทั้งสองจะไม่สามารถทำงาน ในบิวด์เดียวกันได้

ซึ่งแก้ไขได้ด้วยการระบุแฟล็กบิวด์ที่ต้องการเมื่อกำหนดกฎการทดสอบ ดังนี้

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"],
)

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

ทางเลือกที่ซับซ้อนกว่าคือการเขียนสคริปต์ 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

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