Scrittura di test unitari
Scrivere test utilizzando il framework di test unitario di MATLAB® per verificare che gli output degli script, delle funzioni o delle classi di MATLAB siano quelli previsti. Ad esempio, si può verificare che i valori di output effettivi corrispondano a quelli previsti, oppure che gli output abbiano le dimensioni previste e siano del tipo previsto.
Il framework di test unitario consente di scrivere test utilizzando script, funzioni o classi:
I test basati su script forniscono le funzionalità di base per la creazione dei test. È possibile eseguire qualifiche di base utilizzando la funzione
assert
negli script dei test.I test basati sulle funzioni seguono la filosofia dei test xUnit e forniscono ampie funzionalità per la creazione dei test. Ad esempio, è possibile utilizzare funzionalità di qualificazione avanzate, tra cui vincoli, tolleranze e diagnostica dei test.
I test basati sulle classi consentono di accedere a tutte le funzionalità del framework. Ad esempio, è possibile utilizzare impianti di test condivisi, parametrizzare i test e riutilizzare i contenuti dei test.
Per maggiori informazioni, vedere Ways to Write Unit Tests.
Funzioni
assert | Throw error if condition false |
functiontests | Create array of tests from handles to local functions |
Classi
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 |
Argomenti
Scrittura di test basati su classi
- 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.
Scrittura di test parametrizzati
- 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.
Scrittura di test basati sulle funzioni
- 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.
Scrittura di test basati su script
- 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.