Escribir pruebas unitarias
Escriba pruebas utilizando el marco de pruebas unitarias de MATLAB® para comprobar que las salidas de los scripts, funciones o clases de MATLAB son las que espera. Por ejemplo, puede comprobar que los valores de salida reales coincidan con los valores esperados o puede comprobar que las salidas tengan el tamaño y tipo esperados.
El marco de pruebas unitarias permite escribir pruebas utilizando scripts, funciones o clases:
Las pruebas basadas en scripts proporcionan una funcionalidad básica de creación de pruebas. Puede realizar calificaciones básicas usando la función
assert
en los scripts de prueba.Las pruebas basadas en funciones siguen la filosofía de pruebas xUnit y proporcionan una amplia funcionalidad de creación de pruebas. Por ejemplo, puede utilizar prestaciones avanzadas de calificación, incluidas restricciones, tolerancias y diagnósticos de pruebas.
Las pruebas basadas en clases le brindan acceso a todas las funcionalidades del marco. Por ejemplo, puede utilizar accesorios de prueba compartidos, parametrizar pruebas y reutilizar el contenido de las pruebas.
Para obtener más información, consulte Ways to Write Unit Tests.
Funciones
assert | Throw error if condition false |
functiontests | Create array of tests from handles to local functions |
Clases
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 |
Espacios de nombres
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 |
Temas
Escribir pruebas basadas en clases
- 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.
Escribir pruebas parametrizadas
- 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.
Escribir pruebas basadas en funciones
- 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.
Escribir pruebas basadas en 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.