Main Content

Scrittura di test unitari

Scrivere test utilizzando script, funzioni o classi; applicare impianti; parametrizzare i test

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

assertThrow error if condition false
functiontestsCreate array of tests from handles to local functions

Classi

matlab.unittest.FunctionTestCaseTest case for function-based tests
matlab.unittest.TestCaseSuperclass of all test classes
matlab.automation.diagnostics.DiagnosticFundamental interface for diagnostics
matlab.automation.VerbosityVerbosity level enumeration class

Namespaces

matlab.unittestSummary of classes and namespaces in MATLAB unit testing framework
matlab.unittest.constraintsSummary of classes in MATLAB constraints interface
matlab.unittest.fixturesSummary of classes in MATLAB fixtures interface
matlab.unittest.parametersSummary of classes associated with MATLAB unit testing parameters
matlab.unittest.qualificationsSummary of classes in MATLAB qualifications interface

Argomenti

Scrittura di test basati su classi

Scrittura di test parametrizzati

Scrittura di test basati sulle funzioni

  • Write 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