Ich bin neu bei Selen, mit testNG-Framework. Unten ist der Code, den ichSelen - Testng: SoftAssert innerhalb jeder Methode oder innerhalb @afterTest genannt werden
@Test
public void softAssertionTest() {
try{
softAssert.assertTrue(name.equals("HardAssertionTest"));
softAssert.assertEquals(1, 1,"Error 1");
softAssert.assertEquals("a","b","Error ab");
System.out.println("Line after assertions");
} catch (Throwable t){
System.out.println("+++++++++++");
verificationErrors.append(t);
System.out.println("Verification error - method1"+ verificationErrors);
}
}
@Test
public void softAssertionTest2() {
softAssert.assertTrue(name.equals("HardAssertionTest"));
softAssert.assertEquals(3, 4,"Error 3,4");
softAssert.assertEquals("c","d","Error cd");
System.out.println("Line after assertions method 2");
}
@AfterTest
public void afterTest(){
softAssert.assertAll();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
System.out.println(verificationErrorString);
}
}
Q1 zu verstehen versuchen) Ich las es nicht eine gute Praxis ist Behauptungen in Konfigurationsmethoden zu tun. Zeig sie lieber in jeder Testmethode. Daher ändere ich den Code wie unten
@Test
public void softAssertionTest() {
try{
softAssert.assertTrue(name.equals("HardAssertionTest"));
softAssert.assertEquals(1, 1,"Error 1");
softAssert.assertEquals("a","b","Error ab");
System.out.println("Line after assertions");
doSoftAssert();
} catch (Throwable t){
System.out.println("+++++++++++");
verificationErrors.append(t);
System.out.println("Verification error - method1"+ verificationErrors);
}
}
@Test
public void softAssertionTest2() {
try{
softAssert.assertTrue(name.equals("HardAssertionTest"));
softAssert.assertEquals(3, 4,"Error 3,4");
softAssert.assertEquals("c","d","Error cd");
System.out.println("Line after assertions method 2");
doSoftAssert();
} catch (Throwable t){
System.out.println("+++++++++++");
verificationErrors.append(t);
System.out.println("Verification error - method2"+ verificationErrors);
}
}
public void doSoftAssert(){
softAssert.assertAll();
/*String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
System.out.println(verificationErrorString);
}*/
}
Als ich das laufen, ich sehe Aufruf softAssert.assertAll()
von softAssertionTest2()
als auch die Behauptungen in softAssertionTest1()
ausführt, Duplizieren die behaupten. Wie kann ich Behauptungen nur einmal geltend machen?
Q2) Fügt die Fehler an verificationErrorString
der richtige Weg oder ist es ratsam, diese Fehler in einem HashMap
hinzuzufügen?
Vielen Dank für die Anleitung. Ich habe den ersten Teil klar verstanden. Lesen über übersteuernde Assertion. Könnten Sie mir bitte ein Beispiel geben – Jackranda
Werfen Sie einen Blick auf entweder "SofAssert" oder "LoggingAssert" -Klasse von TestNG. Das sollte dir eine klare Vorstellung geben. Vergessen Sie auch nicht, meine Antwort zu akzeptieren, wenn es Ihnen geholfen hat. –
Danke ..wird in diese Klassen schauen. Ich brauchte eine Weile, um herauszufinden, wie ich die Antworten akzeptieren konnte. – Jackranda