How to run a single test case (method) instead of whole test suite (class) using Selenium/TestNG/Maven

元气小坏坏 提交于 2021-02-11 14:12:01

问题


So basically I am wondering how could I run a specific test case (a method) instead of running a whole class.

I am running tests using a combination of Selenium/Maven/TestNG. My current testing setup looks something like this:

simplified command to run tests:

mvn test -DtestSuiteName="test"

contents of test.xml used in the command above:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Selenium Testng Template">
    <test name="TestSuiteTest">
        <classes>
            <class name="listeners.Test_Listener"/>
            <class name="tests.example_test_suite_name"/>
        </classes>
    </test>
</suite>

What the Listener does is basically set up the enviroment for the tests, it starts a pre-configured WebDriver instance and closes it after all the tests are run. It looks something like this:

@Listeners(Listener_Helper)
class Test_Listener implements IExecutionListener {

    @BeforeSuite
    void openBrowser(){
        prepareEnviroment()
    }

    @AfterSuite
    void closeBrowser(){
        cleanUp()
    }

Now to the contents of a example test file. It consists of a class that is somewhat of a test suite, and it that contains several methods, which are somewhat of a test case.

class example_test_suite_name {

    @Test(priority=1)
    void test_name_one() {
        //test instructions
    }

    @Test(priority=2)
    void test_name_two() {
        //test instructions
    }

To summarize, It's not a problem to launch a test suite - a class, but how do I launch a single test case - one method of a class?

Let's say I only wanted to start "test_name_one" but not other methods that class contains. Unfortunately I couldn't find anything related to that on the web.

Any hint regarding this matter would be highly appreciated


回答1:


You can specify test method more in xml file

<groups>
        <run>
            <include name="setName"></include>
        </run>
    </groups>
    <classes>
        <class name="com.journaldev.xml.TestNGXMLTest">
        </class>
    </classes>

Above test suite will only execute methods in setName group, so only test_setName method will be executed.



来源:https://stackoverflow.com/questions/62406674/how-to-run-a-single-test-case-method-instead-of-whole-test-suite-class-using

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!