टेस्ट करना

समस्या की शिकायत करें सोर्स देखें

Bazel में Starlark कोड की जांच करने के कई अलग-अलग तरीके हैं. यह पेज, इस्तेमाल के उदाहरण के हिसाब से मौजूदा सबसे सही तरीकों और फ़्रेमवर्क को इकट्ठा करता है.

जांच के नियम

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() स्टेटमेंट के अलावा, फ़ाइल के दो मुख्य हिस्से होते हैं:

  • हर टेस्ट में, ये शामिल हैं: 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_) होना चाहिए

ध्यान दें कि सभी टारगेट के लेबल, एक ही बिल्ड पैकेज में मौजूद दूसरे लेबल के साथ टकराव कर सकते हैं. इसलिए, टेस्ट के लिए किसी यूनीक नाम का इस्तेमाल करना बेहतर होता है.

असफलता की जांच

यह पुष्टि करना उपयोगी हो सकता है कि कुछ इनपुट के आधार पर या किसी खास स्थिति में कोई नियम काम नहीं करता है. विश्लेषण की जांच करने वाले फ़्रेमवर्क का इस्तेमाल करके ऐसा किया जा सकता है:

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: होनी चाहिए, जैसा कि ऊपर दिखाया गया है.

आर्टफ़ैक्ट की पुष्टि की जा रही है

आपकी जनरेट की गई फ़ाइलें सही हैं या नहीं, यह देखने के लिए मुख्य तरीके हैं:

  • आपके पास शेल, Python या किसी दूसरी भाषा में टेस्ट स्क्रिप्ट लिखने और सही *_test नियम टाइप का टारगेट बनाने का विकल्प है.

  • आपको जिस तरह की जांच करनी है उसके लिए किसी खास नियम का इस्तेमाल किया जा सकता है.

टेस्ट टारगेट का इस्तेमाल करना

आर्टफ़ैक्ट की पुष्टि करने का सबसे आसान तरीका है, स्क्रिप्ट लिखना और अपनी BUILD फ़ाइल में *_test टारगेट जोड़ना. जिन आर्टफ़ैक्ट की आपको जांच करनी है वे इस टारगेट के लिए, डेटा डिपेंडेंसी होने चाहिए. अगर पुष्टि करने वाले लॉजिक को एक से ज़्यादा टेस्ट के लिए फिर से इस्तेमाल किया जा सकता है, तो यह एक ऐसी स्क्रिप्ट होनी चाहिए जिसमें कमांड लाइन तर्क हों. इन्हें टेस्ट टारगेट के 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 की सुविधाओं का टेस्ट करना

Skylib के unittest.bzl फ़्रेमवर्क का इस्तेमाल यूटिलिटी फ़ंक्शन की जांच करने के लिए किया जा सकता है. इसका मतलब है कि ऐसे फ़ंक्शन जो न तो मैक्रो हैं और न ही नियम लागू करते हैं. unittest.bzl की analysistest लाइब्रेरी के बजाय, unittest का इस्तेमाल किया जा सकता है. ऐसे टेस्ट सुइट के लिए, सुविधा फ़ंक्शन 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 के टेस्ट देखें.