パラメーター化

パラメーター化されたテストスイートでは、異なる入力で同じテストが実行されます。

テストスイートのパラメーター化は、次のように設定レベルで定義されます。

<munit:config>
    <munit:parameterizations>
        <munit:parameterization name="firstParameterization">
            <munit:parameters>
                <munit:parameter propertyName="name" value="Robert"/>
                <munit:parameter propertyName="lastname" value="Plant"/>
            </munit:parameters>
        </munit:parameterization>
        <munit:parameterization name="secondParameterization">
            <munit:parameters>
                <munit:parameter propertyName="name" value="Jimmy"/>
                <munit:parameter propertyName="lastname" value="Page"/>
            </munit:parameters>
        </munit:parameterization>
    </munit:parameterizations>
</munit:config>

テストスイートは 2 回実行されます。最初は ​firstParameterization​ パラメーターで実行されます。2 回目は ​secondParameterization​ パラメーターで実行されます。

たとえば、フローのペイロードを設定するテストがある場合、次のような結果が想定されます。

パラメーター化の例
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="http://www.mulesoft.org/schema/mule/core"
      xmlns:munit="http://www.mulesoft.org/schema/mule/munit"
      xmlns:munit-tools="http://www.mulesoft.org/schema/mule/munit-tools"
      xsi:schemaLocation="
        http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
        http://www.mulesoft.org/schema/mule/munit http://www.mulesoft.org/schema/mule/munit/current/mule-munit.xsd
        http://www.mulesoft.org/schema/mule/munit-tools http://www.mulesoft.org/schema/mule/munit-tools/current/mule-munit-tools.xsd">

    <munit:config name="parameterized-test.xml">
        <munit:parameterizations>
            <munit:parameterization name="sumThree">
                <munit:parameters>
                    <munit:parameter propertyName="input" value="#[1]"/>
                    <munit:parameter propertyName="output" value="#[2]"/>
                </munit:parameters>
            </munit:parameterization>
            <munit:parameterization name="sumFive">
                <munit:parameters>
                    <munit:parameter propertyName="input" value="#[2]"/>
                    <munit:parameter propertyName="output" value="#[4]"/>
                </munit:parameters>
            </munit:parameterization>
        </munit:parameterizations>
    </munit:config>

    <munit:test name="expectFlowResult">
        <munit:behavior>
            <set-payload value="${input}"/>
        </munit:behavior>
        <munit:execution >
            <flow-ref name="myFlow"/>
        </munit:execution>
        <munit:validation>
            <munit-tools:assert-equals actual="#[payload]" expected="${output}" />
        </munit:validation>
    </munit:test>

</mule>

ファイルからのパラメーター化

異なるテストスイートで同じパラメーター化された値を使用するには、パラメーター化の値を Mule アプリケーションプロジェクトの ​/test/resources​ ディレクトリの YAML ファイルに保存する必要があります。

parameterizations.yaml の例
firstParameterization:
    name: "Robert"
    lastname: "Plant"

secondParameterization:
    name: "Jimmy"
    lastname: "Page"

次に、それをテストスイート設定から参照します。

ファイルからのパラメーター化の例
<munit:config>
    <munit:parameterizations file="parameterizations.yaml" />
</munit:config>

両方の方法を組み合わせて、パラメーター化された値をテストスイートに追加できます。

ファイルおよびテストスイート設定からのパラメーター化の例
<munit:config name="parameterization-from-file-test.xml">
    <munit:parameterizations file="parameterizations.yaml" >
        <munit:parameterization name="thirdParameterization">
            <munit:parameters>
                <munit:parameter propertyName="name" value="John"/>
                <munit:parameter propertyName="lastname" value="Bonham"/>
            </munit:parameters>
        </munit:parameterization>
    </munit:parameterizations>
</munit:config>

YAML ファイルとテストスイート設定で同じパラメーター化キーを定義した場合、テストスイートのパラメーター化が YAML のパラメーター化よりも優先されます。