2017-02-26 27 views
-1

我想測試它拋出錯誤一類,但我用Matlab 2011B和我沒有找到matlab.unittest(有matlab.unittest.TestSuite.fromFile)。單元測試Matlab的2011版

我能用什麼?

+1

你可以使用MATLAB的升級。 – Adriaan

+0

請問這個工作https://se.mathworks.com/matlabcentral/fileexchange/47302-xunit4 –

回答

0

一種方法是把它寫爲script based test。這種升級方式可以在新版本中使用測試框架。同時,您可以通過調用腳本來運行測試。

如果你現在不能升級,那麼你可以寫類似下面的輔助函數來測試腳本中的這些錯誤:

function assertError(fcn, errorID) 

e = MException.empty; 
try 
    fcn(); 
catch e 
end 
assert(~isempty(e), 'No error occurred. Expected an error with the id "%s"', errorID); 
assert(strcmp(e.identifier, errorID), ... 
    'Wrong error occurred. Expected id "%s", but id "%s" was thrown.', ... 
    errorID, e.identifier); 

爲了測試這一點:

>> assertError(@()error('some:id','Some message'), 'some:id') % no failure 
>> assertError(@()disp(5), 'some:id') 
    5 

Error using assertError (line 8) 
No error occured. Expected an error with the id "some:id" 

>> assertError(@()error('other:id','Some message'), 'some:id') 
Error using assertError (line 9) 
Wrong error occurred. Expected id "some:id", but id "other:id" was thrown. 

>> 
+0

我發現這個鏈接,我嘗試做解釋。但是,也許我錯過了信息,我怎樣才能如果我想波紋管測試代碼做: ' 功能角度= rightTri(邊) 如果(側部(1)<0或側面(2)) 錯誤('邊必須正面') end A = atand(sides(1)/ sides(2)); B = atand(邊(2)/邊(1)); (1)/ sind(A); C = asind(斜邊* sind(A)/邊(1)); angles = [A B C]; 結束 ' 我不覺得以測試代碼拋出正確的錯誤的方式。 – Ccile

+0

我認爲最好的方法是升級,如果可以的話,那麼你有豐富的功能,你需要的所有功能都非常強大。然後你有verifyError(https://www.mathworks.com/help/matlab/ref/matlab.unittest.qualifications.verifiable.verifyerror.html)和/或Throws約束(https://www.mathworks.com/幫助/ MATLAB/REF/matlab.unittest.constraints.throws-class.html)。 –