関数を使用する単純なテスト ケースの記述
メイン関数とローカル テスト関数を含む単一のファイル内でユニット テストを定義して MATLAB® プログラムをテストできます。関数ベースのテストでは、各ローカル関数でソフトウェアの一部を実行し、生成される結果の正確性を検定します。関数ベースのテストの詳細については、関数ベースのユニット テストを参照してください。
この例では、現在のフォルダー内のファイルで定義された関数の正確性を検定する関数ベースのテストを記述する方法を示します。関数 quadraticSolver は、2 次多項式の係数を入力として受け取り、その多項式の根を返します。係数を非数値として指定した場合、関数はエラーをスローします。
function r = quadraticSolver(a,b,c) % quadraticSolver returns solutions to the % quadratic equation a*x^2 + b*x + c = 0. if ~isa(a,'numeric') || ~isa(b,'numeric') || ~isa(c,'numeric') error('quadraticSolver:InputMustBeNumeric', ... 'Coefficients must be numeric.'); end r(1) = (-b + sqrt(b^2 - 4*a*c)) / (2*a); r(2) = (-b - sqrt(b^2 - 4*a*c)) / (2*a); end
テストの作成
関数 quadraticSolver をテストするには、現在のフォルダー内にテスト ファイル quadraticSolverTest.m を作成します。次に、ファイルでメイン関数を定義し、quadraticSolver の実数解と虚数解についてのテストを行う 2 つのローカル関数を定義します。メイン関数とローカル関数の名前は、先頭または末尾が "test" という語 (大文字小文字は区別されない) でなければなりません。また、メイン関数の名前はテスト ファイルの名前に対応している必要があります。
メイン関数の定義
関数ベースのユニット テストを実行するには、すべてのローカル テスト関数を 1 つのテスト配列に収集するメイン関数を定義する必要があります。テスト ファイルでメイン関数 quadraticSolverTest を定義します。メイン関数でfunctiontestsを呼び出してテスト配列 tests を生成します。localfunctionsを functiontests に渡すと、ファイル内のローカル関数への関数ハンドルの cell 配列が自動的に生成されます。
function tests = quadraticSolverTest tests = functiontests(localfunctions); end
ローカル テスト関数の定義
関数 quadraticSolver の実数解と虚数解についてのテストを行うローカル関数をテスト ファイルに追加します。ファイル内のテストの順序は重要ではありません。各ローカル関数は、matlab.unittest.FunctionTestCaseオブジェクトである単一の入力 testCase を受け入れる必要があります。テスト フレームワークが自動的にこのオブジェクトを生成します。このオブジェクトを使用して、関数で値をテストしてエラーに対応するための検定が実行されます。
quadraticSolver が特定の係数に対して正しい実数解を返すことを検証するローカル関数 testRealSolution を定義します。たとえば、方程式 には実数解 と があります。この関数で、この方程式の係数を指定して quadraticSolver を呼び出します。次に、検定関数verifyEqualを使用して、実際の出力 actSolution を想定される出力 expSolution と比較します。
function tests = quadraticSolverTest tests = functiontests(localfunctions); end function testRealSolution(testCase) actSolution = quadraticSolver(1,-3,2); expSolution = [2 1]; verifyEqual(testCase,actSolution,expSolution) end
quadraticSolver が特定の係数に対して正しい虚数解を返すことを検証する 2 つ目のローカル関数 testImaginarySolution を定義します。たとえば、方程式 には虚数解 と があります。この関数で、前の関数と同じように、この方程式の係数を指定して quadraticSolver を呼び出し、検定関数 verifyEqual を使用して実際の出力 actSolution を想定される出力 expSolution と比較します。
function tests = quadraticSolverTest tests = functiontests(localfunctions); end function testRealSolution(testCase) actSolution = quadraticSolver(1,-3,2); expSolution = [2 1]; verifyEqual(testCase,actSolution,expSolution) end function testImaginarySolution(testCase) actSolution = quadraticSolver(1,2,10); expSolution = [-1+3i -1-3i]; verifyEqual(testCase,actSolution,expSolution) end
テスト ファイル内のテストの実行
関数 runtests を使用して、quadraticSolverTest.m ファイルで定義したテストを実行します。この例では、両方のテストにパスします。
results = runtests('quadraticSolverTest.m')Running quadraticSolverTest .. Done quadraticSolverTest __________
results =
1×2 TestResult array with properties:
Name
Passed
Failed
Incomplete
Duration
Details
Totals:
2 Passed, 0 Failed, 0 Incomplete.
0.016572 seconds testing time.
関数runを使用してテストを実行することもできます。
results = run(quadraticSolverTest)
Running quadraticSolverTest .. Done quadraticSolverTest __________
results =
1×2 TestResult array with properties:
Name
Passed
Failed
Incomplete
Duration
Details
Totals:
2 Passed, 0 Failed, 0 Incomplete.
0.0072908 seconds testing time.
参考
runtests | functiontests | localfunctions