2016-03-03 72 views
3

我正在處理的程序執行的計算涉及只能有幾組可能的值的對象。這些參數集是從目錄文件中讀取的。如何基於Matlab單元測試的類參數生成方法參數

舉一個例子說,對象代表汽車,目錄包含每個模型的值集{id:(name,color,power等)}。然而,這些目錄中有很多。

我使用Matlab的unittest包來測試目錄中列出的任何屬性組合的計算是否失敗。我想使用這個包,因爲它提供了一個不合格的條目列表。我已經有了一個測試,爲(硬編碼的)目錄文件生成所有ids的單元數組,並將其用於參數化測試。

現在我需要爲每個目錄文件創建一個新類。我想將目錄文件名稱設置爲類參數,並將其中的條目設置爲方法參數(它是爲所有類參數生成的),但我無法找到將當前類參數傳遞給本地方法以創建方法參數列表。

我該如何做這項工作?

萬一它很重要:我使用Matlab 2014a,2015b或2016a。

+0

每個測試方法都不能遍歷存儲在測試類屬性中的配置文件名嗎? – Jonas

+0

通常,任何時候我循環測試方法內的代碼,我認爲「這應該使用參數化。」我有一個忙碌的早晨,但我會嘗試在今天下午連線回答這個問題。 –

回答

1

我有幾個想法。

  1. 簡短的回答是否定的,因爲在TestParameters被定義爲常量的屬性不能這樣做當前,所以不能在每個ClassSetupParameter值更改。

  2. 但是,對於我爲每個目錄創建一個單獨的類似乎並不是一個壞主意。那個工作流程在哪裏爲你失控?如果需要,您仍然可以通過在測試基類中使用您的內容和目錄文件的抽象屬性來跨這些文件共享代碼。

    classdef CatalogueTest < matlab.unittest.TestCase 
        properties(Abstract) 
         Catalogue; 
        end 
        properties(Abstract, TestParameter) 
         catalogueValue 
        end 
    
        methods(Static) 
         function cellOfValues = getValuesFor(catalog) 
          % Takes a catalog and returns the values applicable to 
          % that catalog. 
         end 
        end 
    
        methods(Test) 
         function testSomething(testCase, catalogueValue) 
          % do stuff with the catalogue value 
         end 
         function testAnotherThing(testCase, catalogueValue) 
          % do more stuff with the catalogue value 
         end 
        end 
    
    end 
    
    
    
    classdef CarModel1Test < CatalogueTest 
    
        properties 
         % If the catalog is not needed elsewhere in the test then 
         % maybe the Catalogue abstract property is not needed and you 
         % only need the abstract TestParameter. 
         Catalogue = 'Model1'; 
        end 
        properties(TestParameter) 
         % Note call a function that lives next to these tests 
         catalogueValue = CatalogueTest.getValuesFor('Model1'); 
        end 
    end 
    

    這是否適用於您正在嘗試做的事?

  3. 當你說方法參數我假設你的意思是「TestParameters」而不是「MethodSetupParameters」正確嗎?如果我正在閱讀你的問題,我不確定這適用於你的情況,但我想提到你可以從你的ClassSetupParameters/MethodSetupParameters中獲取數據到你的測試方法中,方法是在你的類上創建另一個屬性來保存Test中的值[Method | Class]安裝並在Test方法中引用這些值。 像這樣:

    classdef TestMethodUsesSetupParamsTest < matlab.unittest.TestCase 
        properties(ClassSetupParameter) 
         classParam = {'data'}; 
        end 
        properties 
         ThisClassParam 
        end 
    
        methods(TestClassSetup) 
         function storeClassSetupParam(testCase, classParam) 
          testCase.ThisClassParam = classParam;  
         end 
        end 
    
        methods(Test) 
         function testSomethingAgainstClassParam(testCase) 
          testCase.ThisClassParam 
         end 
        end 
    
    end 
    

    當然,在這個例子中,你應該只使用一個TestParameter,但可能會有某些情況下,這可能是有用的。不知道它是否有用。

+0

我對此使用了靜態方法。非常感謝你! – Tim