La traduction de cette page n'est pas à jour. Cliquez ici pour voir la dernière version en anglais.
Écrire des tests unitaires
Écrivez des tests avec le Unit Testing Framework MATLAB® pour vérifier que les sorties des scripts, fonctions ou classes MATLAB sont conformes à vos attentes. Par exemple, vous pouvez vérifier que les valeurs de sortie réelles correspondent à celles attendues ou que les sorties sont de la taille et du type prévus.
Le Unit Testing Framework vous permet d’écrire des tests avec des scripts, des fonctions ou des classes :
Les tests basés sur des scripts offrent des fonctionnalités de création de tests de base. Vous pouvez effectuer des qualifications de base en utilisant la fonction
assert
dans vos scripts de test.Les tests basés sur des fonctions suivent la philosophie de test xUnit et offrent des fonctionnalités étendues de création de tests. Par exemple, vous disposez de capacités de qualification avancées comprenant des contraintes, des tolérances et des diagnostics de test.
Les tests basés sur des classes vous donnent accès à l’ensemble des fonctionnalités du framework. Par exemple, vous pouvez utiliser des dispositifs de test partagés, paramétrer les tests et réutiliser leur contenu.
Pour plus d’informations, consultez Ways to Write Unit Tests.
Fonctions
assert | Throw error if condition false |
functiontests | Create array of tests from handles to local functions |
Classes
matlab.unittest.FunctionTestCase | Test case for function-based tests |
matlab.unittest.TestCase | Superclass of all test classes |
matlab.automation.diagnostics.Diagnostic | Fundamental interface for diagnostics |
matlab.automation.Verbosity | Verbosity level enumeration class |
Namespaces
matlab.unittest | Summary of classes and namespaces in MATLAB unit testing framework |
matlab.unittest.constraints | Summary of classes in MATLAB constraints interface |
matlab.unittest.fixtures | Summary of classes in MATLAB fixtures interface |
matlab.unittest.parameters | Summary of classes associated with MATLAB unit testing parameters |
matlab.unittest.qualifications | Summary of classes in MATLAB qualifications interface |
Rubriques
Écrire des tests basés sur des classes
- Class-Based Unit Tests
Test your MATLAB source code by creating class-based unit tests. - Write Simple Test Case Using Classes
Write class-based unit tests to determine the correctness of your program. - Write Setup and Teardown Code Using Classes
Specify setup and teardown code that runs before and after eachTest
method or allTest
methods in a test class. - Write Tests Using Shared Fixtures
Share fixtures across test classes when creating tests. - Tag Unit Tests
Use test tags to group tests into categories and then run tests with specified tags. Typical test tags identify a particular feature or describe the type of test. - Insert Test Code Using Editor
Write a parameterized test interactively by using the options in the MATLAB Editor. - Table of Verifications, Assertions, and Other Qualifications
Test values and respond to failures using verifications, assumptions, assertions, and fatal assertions. - Ways to Write Unit Tests
Choose a test authoring scheme that best suits your requirements and your preferred workflow.
Écrire des tests paramétrés
- Use Parameters in Class-Based Tests
Use parameters to provide data for your tests to use iteratively. - Create Basic Parameterized Test
Test a function by creating a test that is parameterized in theTest
methods
block. - Create Advanced Parameterized Test
Create a test that is parameterized in theTestClassSetup
,TestMethodSetup
, andTest
methods
blocks. - Define Parameters at Suite Creation Time
Use aTestParameterDefinition
method to define parameters at test suite creation time. - Use External Parameters in Parameterized Test
Use data-driven testing to inject variable inputs into your parameterized test.
Écrire des tests basés sur des fonctions
- Function-Based Unit Tests
A test function is a single MATLAB file that contains a main function and your individual local test functions. Optionally, you can include file fixture and fresh fixture functions. - Write Simple Test Case Using Functions
Write function-based unit tests to determine the correctness of your program. - Write Test Using Setup and Teardown Functions
Write a function-based test with setup and teardown functions that run once in your test file and before and after each test function in the file. - Extend Function-Based Tests
Access additional functionality using function-based tests, including application of fixtures, test selection, programmatic access of test diagnostics, and test runner customization.
Écrire des tests basés sur des scripts
- Write Script-Based Unit Tests
Write a script to test a function that you create. - Write Test Using Live Script
Write a live script that tests a function that you create. - Write Script-Based Test Using Local Functions
Write a script-based test that uses local functions as helper functions. - Extend Script-Based Tests
Access additional functionality using script-based tests, including test selection, programmatic access of test diagnostics, and test runner customization.