Nathan Beach
Nathan Beach

Reputation: 2557

Is there way to span dependencies across Tests in TestNG?

I'm on TestNG 6.8.5. Is there a way to span either dependsOnGroups or dependsOnMethods across Tests, rather than just within a single Test?

I have a Suite configured like this with two Tests and a bunch of other Tests between them:

<suite name="ConfigurationSuite">

    <test name="DeviceMgmtTest_Setup" >
        <classes>
            <class name="com.yay.DeviceMgmtTest">
                <methods>
                    <include name="createDevice" />
                    <include name="discoverDevice" />
                </methods>
            </class>
        </classes>
    </test>

    <!-- A whole bunch of other very sequential
         and long-running tests go here... -->

    <test name="DeviceMgmtTest_TearDown" >
        <classes>
            <class name="com.yay.DeviceMgmtTest">
                <methods>
                    <include name="deleteDevice" />
                </methods>
            </class>
        </classes>
    </test>

</suite>

My test class has methods configured like this:

@Test(groups = { "setup" })
public void createDevice() {
    // do something
}

@Test(groups = { "setup" })
public void discoverDevice() {
    // do something
}

@Test(groups = { "tearDown" }, dependsOnGroups = { "setup" })
public void deleteDevice() {
    // When TestNG reaches this method I get a
    // group "setup" does not exist type exception (see below)
}

But when I run the suite, the dependsOnGroups annotation fails with an error like this:

[ERROR] org.testng.TestNGException:
[ERROR] DependencyMap::Method .... DeviceMgmtTest depends on nonexistent group "setup"

Maybe I'm not understanding something correctly about how to configure my Suite (I'm very new to TestNG), but separating these areas of concern out into separate Tests makes sense to me.

Thanks for any guidance or suggestions!

EDIT: One solution I'm considering is grabbing the TestNG ITestContext and navigating to the parent Suite, and then checking the status of a previous test. I just thought there might be a more elegant TestNG convention.

Upvotes: 3

Views: 6529

Answers (2)

niharika_neo
niharika_neo

Reputation: 8531

I think based on what your sample code is ..all you want is that your teardown should execute after your setup and your tests. You might want to chuck dependson altogether and try using preserve-order = true. As such you need not even set it coz that's the default and testng runs your tests in the order that they are found in your xml.

Upvotes: 1

Nathan Merrill
Nathan Merrill

Reputation: 8386

It is impossible to depend on a method from different classes.

However, it is definitely possible to depend on a group from different classes. If you want to use XML, this is the documentation for that: http://testng.org/doc/documentation-main.html#dependencies-in-xml

Upvotes: 3

Related Questions