Main Content

共有フィクスチャを使用するテストの記述

matlab.unittest.TestCaseクラスの SharedTestFixtures 属性を使用して、テスト フィクスチャをテスト クラス全体で共有することができます。一緒に実行されるテスト クラスでフィクスチャを共有すると、テスト フレームワークによってすべてのテスト クラス用にフィクスチャが 1 回セットアップされ、すべてのテスト クラスが実行されてからそのフィクスチャが破棄されます。代わりに、各クラスの TestClassSetup methods ブロックを使用してフィクスチャを指定すると、テスト フレームワークによって各テスト クラスの実行前にフィクスチャがセットアップされ、実行後に破棄されます。

この例では、テストの作成時に共有フィクスチャを使用する方法を示します。ソース コードが含まれているフォルダーをパスに追加するフィクスチャを 2 つのテスト クラスで共有する方法について説明します。テスト クラスはこのフィクスチャを使用して、テストに必要なソース コードにアクセスします。

例を開いて、ソースおよびテスト コードを現在のフォルダーで使用できるようにします。

openExample("matlab/WriteTestsUsingSharedTestFixturesExample")

DocPolynomTest クラスの定義

このコードでは、DocPolynomTest クラス定義ファイルの内容を示します。このファイルは共有フィクスチャを使用して、DocPolynom クラスを定義しているフォルダーにアクセスします。DocPolynom クラスの詳細とクラス コードの表示方法については、クラスによる多項式の表現を参照してください。

classdef (SharedTestFixtures={ ...
        matlab.unittest.fixtures.PathFixture( ...
        fullfile("..","fixture_example_source"))}) ...
        DocPolynomTest < matlab.unittest.TestCase

    properties
        TextToDisplay = "Equation under test: "
    end

    methods (Test)
        function testConstructor(testCase)
            p = DocPolynom([1 0 1]);
            testCase.verifyClass(p,?DocPolynom)
        end

        function testAddition(testCase)
            p1 = DocPolynom([1 0 1]);
            p2 = DocPolynom([5 2]);
            actual = p1 + p2;
            expected = DocPolynom([1 5 3]);
            diagnostic = [testCase.TextToDisplay ...
                "(x^2 + 1) + (5*x + 2) = x^2 + 5*x + 3"];
            testCase.verifyEqual(actual,expected,diagnostic)
        end

        function testMultiplication(testCase)
            p1 = DocPolynom([1 0 3]);
            p2 = DocPolynom([5 2]);
            actual = p1 * p2;
            expected = DocPolynom([5 2 15 6]);
            diagnostic = [testCase.TextToDisplay ...
                "(x^2 + 3) * (5*x + 2) = 5*x^3 + 2*x^2 + 15*x + 6"];
            testCase.verifyEqual(actual,expected,diagnostic)
        end
    end
end

BankAccountTest クラスの定義

このコードでは、BankAccountTest クラス定義ファイルの内容を示します。このファイルは共有フィクスチャを使用して、BankAccount クラスを定義しているフォルダーにアクセスします。BankAccount クラスの詳細とクラス コードの表示方法については、連携するクラスの開発を参照してください。

classdef (SharedTestFixtures={ ...
        matlab.unittest.fixtures.PathFixture( ...
        fullfile("..","fixture_example_source"))}) ...
        BankAccountTest < matlab.unittest.TestCase

    methods (Test)
        function testConstructor(testCase)
            b = BankAccount(1234,100);
            testCase.verifyEqual(b.AccountNumber,1234, ...
                "Constructor must correctly set account number.")
            testCase.verifyEqual(b.AccountBalance,100, ...
                "Constructor must correctly set account balance.")
        end

        function testConstructorNotEnoughInputs(testCase)
            import matlab.unittest.constraints.Throws
            testCase.verifyThat(@()BankAccount,Throws("MATLAB:minrhs"))
        end

        function testDeposit(testCase)
            b = BankAccount(1234,100);
            b.deposit(25)
            testCase.verifyEqual(b.AccountBalance,125)
        end

        function testWithdraw(testCase)
            b = BankAccount(1234,100);
            b.withdraw(25)
            testCase.verifyEqual(b.AccountBalance,75)
        end

        function testNotifyInsufficientFunds(testCase)
            callbackExecuted = false;
            function testCallback(~,~)
                callbackExecuted = true;
            end

            b = BankAccount(1234, 100);
            b.addlistener("InsufficientFunds",@testCallback);

            b.withdraw(50)
            testCase.assertFalse(callbackExecuted, ...
                "The callback should not have executed yet.")
            b.withdraw(60)
            testCase.verifyTrue(callbackExecuted, ...
                "The listener callback should have fired.")
        end
    end
end

テストの実行

現在のフォルダーおよびそのサブフォルダーでテストを実行します。テスト フレームワークによって共有テスト フィクスチャがセットアップされ、BankAccountTest クラスおよび DocPolynomTest クラスでテストが実行されます。テストの実行後にフィクスチャが破棄されます。この例では、すべてのテストがパスします。

runtests("IncludeSubfolders",true);
Setting up PathFixture
Done setting up PathFixture: Added 'C:\work\WriteTestsUsingSharedTestFixturesExample\fixture_example_source' to the path.
__________

Running BankAccountTest
.....
Done BankAccountTest
__________

Running DocPolynomTest
...
Done DocPolynomTest
__________

Tearing down PathFixture
Done tearing down PathFixture: Restored the path to its original state.
__________

参考

| |

関連するトピック