如何在TestNG

时间:2018-03-09 13:29:41

标签: selenium-webdriver testng assertions

自动化保险申请。对于硒网络驱动程序测试的Writig TestNG断言,以验证策略的开始日期,这是默认的今天的日期。

但在某些情况下,明天的日期可能取决于保险公司和计划。我想在断言中加入这个。但我想验证这两个中的任何一个,开始日期=今天或明天。想要结合以下两个断言。

我可以通过创建第三个布尔变量来检查日期以查看它是否与其中一个匹配并使布尔值为true并执行布尔断言,任何人都知道在testNG中直接执行此操作的任何其他方法。

1.    Assert.assertEquals(start_date, date_today, "Assertion failed-  Start Date");

2.    Assert.assertEquals(start_date, date_tomorrow, "Assertion failed-  Start Date");

在junit,我们有类似

的东西
assertThat(result, isOneOf("value1", "value2"));

可以断言类似的情况。

我们可以在testNG中进行相同的操作吗?

2 个答案:

答案 0 :(得分:2)

this similar question一样,不是含蓄的。 JUnit中的BTW assertThat可能依赖于Hamcrest匹配器,也许是旧版本。无论如何,你至少有以下选择:

<强>依赖关系:

    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-core</artifactId>
        <version>1.3</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.assertj</groupId>
        <artifactId>assertj-core</artifactId>
        <version>3.9.0</version>
        <scope>test</scope>
    </dependency>

<强>代码:

import org.testng.annotations.Test;

import java.time.LocalDate;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.CoreMatchers.anyOf;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.testng.Assert.assertTrue;

public class SomeTest {
    @Test // compare strings (usually that's what you get as values from a webpage)
    public void shouldCheckMultipleStringValues() {
        // simulate some data
        String myText = "my text";

        // TestNG
        assertTrue(myText.equals("my text") || myText.equals("your text") || myText.equals("his text"));

        //Hamcrest
        assertThat(myText, anyOf(equalTo("my text"), equalTo("your text"), equalTo("his text")));

        // AssertJ
        assertThat(myText).isIn("my text", "your text", "his text");
    }

    @Test // but you can even compare dates if you need to
    public void shouldCheckMultipleDateValues() {
        // simulate some date
        LocalDate myDate = LocalDate.now();

        // simulate a bunch of expected dates
        LocalDate yesterday = LocalDate.now().minusDays(1);
        LocalDate now = LocalDate.now();
        LocalDate tomorrow = LocalDate.now().plusDays(1);

        // TestNG
        assertTrue(myDate.equals(yesterday) || myDate.equals(now) || myDate.equals(tomorrow));

        //Hamcrest
        assertThat(myDate, anyOf(equalTo(yesterday), equalTo(now), equalTo(tomorrow)));

        // AssertJ
        assertThat(myDate).isIn(yesterday, now, tomorrow);
        assertThat(myDate).isBetween(yesterday, tomorrow);
    }
}

答案 1 :(得分:1)

我会计算天数并对该数字做一个断言:

long days = ChronoUnit.DAYS.between(start_date, date_today);
assertTrue((days >= 0) && (days <= 1));