2017-08-25 82 views
1

我正在做一個特殊的junit測試,參數是由用戶在應用程序的前端引入並在應用程序的後端接收的。我想生成使用該信息作爲參數的junit測試。Java將參數動態地傳遞給junit測試

我看到一些指南(如mykong指南和教程點),但其中大多數使用靜態參數化,我想要一些動態的東西。我已經嘗試過使用junit註釋,做一組或將參數傳遞給junit類,使用mockito方法但沒有將其作爲動態過程工作

有人可以指向正確的方向嗎?

現在我有類似的東西

public void run (Object foo) //Class that contains the information introduced by the user 
JUnitCore junit1 = new JUnitCore(); 
Result result4 = JUnitCore.runClasses(GeneratedTest.getClass()); //Junit class 

//I tried: do a setFoo on the GeneratedTest ; pass the foo on the constructor; 

for (Failure failure : result4.getFailures()) { 
        System.out.println(failure.toString()); 
} 
+0

動態測試可以用「測試工廠」構建 - https://dzone.com/articles/junit-5-dynamic-tests-generate-tests-at-run-time –

+4

您正在搜索的行爲不是什麼一個單元測試旨在用於 –

+0

我還在考慮你可能會濫用單元測試來達到一個錯誤的目的。你能向我們解釋你的確切用例嗎? –

回答

1

也許不是最好的解決辦法,但也許可以接受的解決辦法:

  • 生成你的單元測試,以便它從系統屬性獲取參數
  • 在其自己的JVM中運行生成的JUnit測試,並在命令行上傳遞參數/屬性
+0

這是如何在maven的自動化環境中完成的:https://stackoverflow.com/questions/32120909/how-to-pass-parameter-to-maven-test – jschnasse