簡體   English   中英

如何為junit測試設置測試名稱-但不使用測試方法的名稱?

[英]How to set test name for junit test - but not using name of test method?

我有復雜的junit測試。 我想設置此測試的名稱/說明,僅在測試失敗時才會顯示,否則,我只需要簡單的名稱。

我可以用常見的方法來做,並使用測試方法的名稱作為對此方法的描述。

@Test
/**
 * Should return something when some parameter is set to specific value 
 * and other parameter is set to another specific value and everything is
 * executed in specific context under specific environment
 */
public void test42() {
    ....
}

@Test()
//not like that
public void shouldReturnSomethingWhenSomeParameterIsSetToSpecificValueAndOtherParameterIs SetToAnotherSpecificValueAandEverythingIsExecutedInSpecificContextUnderSpecificEnvironment() {
    ....
}

可以預期的是,在測試失敗時,將公開命令的描述。 像這樣:

[INFO] Results:
[INFO]
[ERROR] Failures: 
[ERROR]   FooTest.test42:110 
[ERROR]   > Should return something when some parameter is set to specific value 
[ERROR]   > and other parameter is set to another specific value and everything is
[ERROR]   > executed in specific context under specific environment
Expecting:
  <[8, 3]>
to contain only:
  <[9, 2]>
elements not found:
  <[9, 2]>
and elements not expected:
  <[8, 3]>

[INFO]
[ERROR] Tests run: 85, Failures: 1, Errors: 0, Skipped: 0

@Test接口沒有任何此類值。 但是是斷言錯誤使測試失敗,因此您可以向特定的斷言添加一條消息,例如:

assertTrue("Those are not equal", 0 == 1);

或者,例如,使用hamcrest,您可以通過一個原因:

assertThat("Those should be equal", 0, is(equalTo(0)));

消息將在失敗時打印。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM