简体   繁体   中英

Dataproviders and Asserts

When using DataProviders, on TestNG, my test method has asserts that will fail since the data passed in navigates to a different url. Is there a way to work around this, ie a way for the data to only be injected to certain/specific asserts?


Instead of testing one scenario with different data, I am instead testing multiple scenarios with different data which is where my conflict arises.


@DataProvider(name = "VINNumbers")
public String[][] VINNumbers() {
    return new String[][] {
            {"2T1BU4ECC834670"},
            {"1GKS2JKJR543989"},
            {"2FTDF0820A04457"}
    };
}

@Test(dataProvider = "VINNumbers")
public void shouldNavigateToCorrespondingVinEnteredIn(String VIN) {
    driver.get(findYourCarPage.getURL() + VIN);
    Assert.assertTrue(reactSRP.dealerListingMSRPIsDisplayed());
}

The assert test whether or not the page has an MSRP displayed, but not all dataproviders will have an MSRP displayed so it will fail. The only dataprovider that has it is the first array. Is there a way for dataproviders to be called to specific asserts?

If depending on the VIN, MSRP is displayed or not (boolean), you could for example create a provider the way it provides VIN and expected result:

@Test(dataProvider = "VINNumbers")
public void shouldNavigateToCorrespondingVinEnteredIn(String VIN, boolean isMSRPDisplayed) {
    // act
    // assert
    assertThat(reactSRP.dealerListingMSRPIsDisplayed()).is(isMSRPDisplayed);
}

This way you end up with an provider like below:

{
    {"2T1BU4ECC834670", true},
    {"1GKS2JKJR543989", false},
    {"2FTDF0820A04457", true},
}

In my opinion this is acceptable for simple cases. To make assertion more readable, I would add a custom message to it that is also parameterized.

I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM