0

在我的 webdriver 脚本中,我有三种方法

setup, test and tearDown

遵循junit约定。

test方法中,我很少有这样的断言

@Test
public void testStudentHome() throws Exception {
    String classCode = "I6OWW";
    Utilities.studentSignin(driver, baseUrl);
    assertEquals(true, sth.openNotification());
    assertEquals("My Scores", sth.myScores(true));
}

sth是我在其上执行测试并且在setup方法中创建的 PageObject。

我从这样的main方法中调用所有这三种方法:

public static void main(String[] args) {
        StudentHomeTest sht = new StudentHomeTest();
        try {
            sht.setup();
            sht.testStudentHome();
            sht.tearDown();
        } catch (Exception ex) {
            Logger.getLogger(StudentHomeTest.class.getName()).log(Level.SEVERE, null, ex);
            sht.tearDown();
        }
    }

现在,如果某个断言失败,则在运行测试时,测试方法应该(这是我所期望的)抛出异常并且该main方法应该调用该tearDown方法。但这不会发生。并且浏览器窗口继续停留在那里。我正在使用 netbeans ide 来运行测试。

4

2 回答 2

2

遵循junit公约

如果您遵循 jUnit 约定,那么您将知道拆卸方法属于 @After 方法,因为此方法将始终在您的测试之后运行。

@After使用jUnit 注释创建一个新方法。

@After
public void tearDown() {
  sht.tearDown();
}

编辑

你知道吗,我相信你遇到了assertEqualsjUnit 中的一个经典问题。

这个答案中窃取...:

JUnit 调用 .equals() 方法来确定方法 assertEquals(Object o1, Object o2) 中的相等性。

因此,使用 assertEquals(string1, string2) 绝对是安全的。(因为字符串是对象)

--
不要assertEquals在这些调用上使用,assertTrue()而是使用。

assertTrue(sth.openNotification());
assertTrue("My Scores".equals(sth.myScores(true)));
于 2013-10-17T17:15:53.960 回答
1

AssertionError不扩展Exception- 它是一个Throwable.

但无论如何,你应该有

    try {
        sht.setup();
        sht.testStudentHome();
    } finally {
        sht.tearDown();
    }

不需要 catch 块。main可以throw Exception

于 2013-10-17T09:39:09.170 回答