How to read in arguments passed via ant to testng.

2019-06-14 05:24发布

问题:

Here is the scenario:

Have a shell script that calls ant with one argument. The ant in turn executes testng.xml (suite file) passing the same argument and testng in turn executes the test within passing the same argument.

In my case, I am passing the browser string eg.(firefox, iexplore) argument that will specify which browser test will run on. I want to be able to have my test result output tell me which browser the test run in.

I grab the argument from command line in ant by so:

...

<sysproperty key="browser" value="${browser}"/>

I was thinking that since ant calls testng.xml, i can do the same in testng.xml

I went to testng.xml and did something like:

<suite name="AcceptanceSuite_${browser}">
<test name="Acceptance Test_${browser}" >

I hope i didnt lose anybody. Not the best in explaining things but simply need away of capturing this argument in testng.xml and including that in the suite name

回答1:

In build.xml file, set delegateCommandSystemProperties=true in testng tag and add <sysproperty key="property" value="${property}"/> under testng tag as follows:

<target name="execute" depends="compile">
        <testng delegateCommandSystemProperties="true">
            <sysproperty key="property" value="${property}"/>
            <xmlfileset dir="${basedir}" includes="testng.xml" />
        </testng>
    </target>

In testng file add "key" and "value" in parameter tag as follows:

<parameter name="property" value="${property}"></parameter>

Run ant from command-line and pass parameter value as follows:

ant -Dproperty=true execute

This should work.



回答2:

I think it should work with <sysproperty> if you set delegateCommandSystemProperties to true and nest the <sysproperty> within <testng>

Not sure if you have nested the <sysproperty> ?



标签: xml ant shell sh