Ant build.xml phpunit - ant

I have a ant build.xml (below). I am able to run phpunit fine from the command line as follows:
D:> phpunit --verbose --testdox-html logs\today.html runtest
This runs all my phpunit tests within the folder d:\runtest.
My problem is when I run my build.xml as 'ant build' it tries to execute a file called runtest.php the output from ant is below:
D:\>ant build
Buildfile: D:\build.xml
check_os:
if_windows:
if_unix:
prepare:
phpunit:
[exec] PHPUnit 3.6.11 by Sebastian Bergmann.
[exec]
[exec] Cannot open file "runtest.php".
BUILD FAILED
D:\build.xml:48: exec returned: 1
Total time: 2 seconds
My Build.xml is as follows:
<!-- This project launches the test generator and execute all phpunit selenium tests -->
<project name="proj" default="build" basedir="">
<!--Get environment variables -->
<property environment="env" />
<property name="logFolder" value="${basedir}\logs"/>
<property name="testFolder" value="${basedir}\runtest"/>
<property name="test" value="**" />
<condition property="pattern" value="runtest/*.php">
<os family="windows" />
</condition>
<tstamp/>
<!-- Check Operating system to set phpunit path-->
<target name="check_os">
<condition property="isWindows">
<os family="windows" />
</condition>
<condition property="isLinux">
<os family="unix" />
</condition>
</target>
<target name="if_windows" depends="check_os" if="isWindows">
<property name="exe.phpunit" value="C:\\Program Files\\PHP\\phpunit.bat"/>
</target>
<target name="if_unix" depends="check_os" if="isLinux">
<property name="exe.phpunit" value="${env.PHP_HOME}/includes/PHPUnit-3.2.0/PHPUnit" />
</target>
<target name="prepare" depends="if_windows, if_unix">
<mkdir dir="${logFolder}"/>
</target>
<target name="phpunit">
<!-- Check if folder empty -->
<fileset id="fileset.test" dir="${testFolder}">
<include name="*.*"/>
</fileset>
<fail message="Files not found">
<condition>
<resourcecount refid="fileset.test" when="less" count="1"></resourcecount>
</condition>
</fail>
<!-- Execute phpunit tests -->
<exec executable="${exe.phpunit}" failonerror="true" dir="runtest">
<arg line="--verbose --testdox-html '${logFolder}\phpunit-report-${TODAY}.html' runtest" />
</exec>
</target>
<target name="build" depends="prepare,phpunit"/>
</project>

The problem was I specified dir="runtest" once I removed this from the Execute line it worked.

<target name="phpunit" unless="phpunit.done" depends="prepare" description="Run unit tests with PHPUnit">
<exec executable="${phpunit}" failonerror="true" dir="${basedir}/classes/tests/" resultproperty="result.phpunit">
${phpunit} - needs Path of phpunit installation, you can get it from where is phpunit command in linux
dir="${basedir}/classes/tests/" - Need only the folder path where your php application is present
<arg line="UserTest ${basedir}/classes/tests/userTest.php" />
line="UserTest ${basedir}/classes/tests/userTest.php" - Here UserTest is class name and ${basedir}/classes/tests/userTest.php it is path of test class file
<arg value="--configuration"/>
<arg path="${basedir}/classes/tests/UnitTest.xml"/>
path="${basedir}/classes/tests/UnitTest.xml" - Path of xml file
</exec>
<property name="phpunit.done" value="true"/>
</target>

Related

TestNG test-output folder getting generated on Desktop when expected to be in project folder

I have a setup of Selenium WebDriver + TestNG + Ant framework in my automation project. Running webdriver + TestNG tests from Ant using build.xml was working absolutely fine a few months ago. TestNG was generating the test-output folder in the project directory as expected. Now when I run my testng tests from ANT it's generating the default report folder test-output on my Desktop (home/user/Desktop). I don't know why it is happening.
This is my build.xml file:
<project name="InitialConfigProject" default="start" basedir=".">
<!-- ========== Initialize Properties =================================== -->
<property environment="env"/>
<property file="./app.properties"/>
<property name="ws.home" value="${basedir}"/>
<property name="test.dest" value="${ws.home}/build"/>
<property name="test.src" value="${ws.home}/src"/>
<property name="browser" value="/usr/bin/google-chrome"/>
<property name="mail_body_file" value="${basedir}/email_body.txt"/>
<property name="buildID" value="IND3.2.0"/>
<property name="sendmailscript_path" value="${basedir}/sendmail.sh"/>
<property name="mail_subject" value="Automated_test_execution_of_${buildID}"/>
<!-- ====== Set the classpath ==== -->
<target name="setClassPath" unless="test.classpath">
<path id="classpath_jars">
<fileset dir="${ws.home}/lib" includes="*.jar"/>
</path>
<pathconvert pathsep=":" property="test.classpath" refid="classpath_jars"/>
</target>
<!-- ============ Initializing other stuff =========== -->
<target name="init" depends="setClassPath">
<tstamp>
<format property="timestamp" pattern="dd/MM/yyyy hh:mm aa" />
</tstamp>
<!--
<condition property="ANT"
value="${env.ANT_HOME}/bin/ant.bat"
else="${env.ANT_HOME}/bin/ant">
<os family="windows" />
</condition> -->
<property name="build.log.dir" location="${basedir}/buildlogs" />
<mkdir dir="${build.log.dir}"/>
<property name="build.log.filename" value="build_${timestamp}.log"/>
<record name="${build.log.dir}/${build.log.filename}" loglevel="verbose" append="false"/>
<echo message="build logged to ${build.log.filename}"/>
<echo message="Loading TestNG.." />
<taskdef name="testng" classpath="${test.classpath}" classname="org.testng.TestNGAntTask" />
</target>
<!-- cleaning the destination folders -->
<target name="clean">
<delete dir="${test.dest}"/>
</target>
<!-- compiling files -->
<target name="compile" depends="init, clean" >
<delete includeemptydirs="true" quiet="true">
<fileset dir="${test.dest}" includes="**/*"/>
</delete>
<echo message="making directory..."/>
<mkdir dir="${test.dest}"/>
<copy file="${ws.home}/app.properties" todir="${ws.home}/build" />
<copy file="${ws.home}/resources/testdata/testDataSet1.properties" todir="${ws.home}/build" />
<echo message="compiling source files..."/>
<javac
debug="true"
destdir="${test.dest}"
srcdir="${test.src}"
target="1.6"
classpath="${test.classpath}"
includeantruntime="true"
>
</javac>
</target>
<!-- run -->
<target name="run" depends="compile">
<testng outputdir="${ws.home}/test-output" classpath="${test.classpath}:${test.dest}" suitename="Praxify Sanity Suite">
<xmlfileset dir="${ws.home}" includes="testng.xml"/>
</testng>
</target>
<!-- ========== Generating reports using XSLT utility ============== -->
<target name="testng-xslt-report">
<delete dir="${basedir}/testng-xslt">
</delete>
<mkdir dir="${basedir}/testng-xslt">
</mkdir>
<xslt in="${basedir}/test-output/testng-results.xml" style="${basedir}/testng-results.xsl" out="${basedir}/testng-xslt/index.html"
processor="SaxonLiaison">
<param expression="${basedir}/testng-xslt/" name="testNgXslt.outputDir" />
<param expression="true" name="testNgXslt.sortTestCaseLinks" />
<param expression="FAIL,SKIP,PASS,CONF,BY_CLASS" name="testNgXslt.testDetailsFilter" />
<param expression="true" name="testNgXslt.showRuntimeTotals" />
<classpath refid="classpath_jars"></classpath>
</xslt>
</target>
<!-- Starting point of the execution, should be dependent on target "run".
Target sequence will be:
start (not_execute) ==> run (not_execute) ==> compile (not_execute) ==> init (execute) ==> clean (execute)
start (execute) <== testng-xslt-report (execute) <== run (execute) <== compile (execute) <==
Suitable for ANT 1.7. Currently using this ====================== -->
<target name="start" depends="run, testng-xslt-report">
<tstamp prefix="getTime">
<format property="TODAY" pattern="MM-dd-yyyyhhmmaa"/>
</tstamp>
<echo message="sending report as mail...."/>
<property name="execution_time" value="${buildID}_${getTime.TODAY}"/>
<property name="dest_file" value="/home/xtremum/Reports/${execution_time}.zip"/>
<zip destfile="/home/xtremum/Reports/${execution_time}.zip" basedir="${basedir}/testng-xslt"/>
<property name="report_attachment_file" value="${dest_file}"/>
<exec executable="${sendmailscript_path}" newenvironment="false">
<arg value="${mail_subject}"/>
<arg value="${mail_recipient}"/>
<arg value="${report_attachment_file}"/>
<arg value="${mail_body_file}"/>
</exec>
</target>
Just for the record:
1. I am using Eclipse Juno.
2. I have installed TestNG plugin on Eclipse so that I can run tests directly from eclipse by right clicking on testng.xml and going for Run.
3. I have installed ANT 1.7 on my Ubuntu machine and have set my ANT_HOME pointing to /usr/share/ant. And I looked up in Windows -> Preferences -> Ant -> Runtime -> Ant Home Entries (Default) and they seem to have references to ant 1.8.3 libraries (JARS) which are inside the Eclipse package (eclipse/plugins/). Is there anything wrong here?
4. I am running the tests via ant from eclipse and not from command line.
I am not getting any build errors. Tests are getting executed but the test-output folder is getting created on Desktop. Any help?
If you are running through the testng plugin option, the output folder would be the one you specify in Project->Properties->TestNG->OutputDirectory

How to conditionally execute batch scripts for different platforms using Ant?

I am attempting to use an Ant build script to build a project that already has nmake (Visual Studio) build scripts. Rather than redo the entire build script, I would like to have Ant reuse the existing scripts.
So, I have something like this which works for Windows Mobile 6 ARMV4I builds:
<project ...>
<target name="-BUILD.XML-COMPILE" depends="-init, -pre-compile">
<exec executable="cmd">
<arg value="/c"/>
<arg line='"${g.path.project}\build-wm6-armv4i.bat"'/>
</exec>
<antcall target="-post-compile" inheritall="true" inheritrefs="true" />
</target>
</project>
But I would also like it to work for other platforms like Win32 x86 and Windows CE6 x86.
How can I have the Ant script discriminate which batch file it should execute to perform the build?
The <os> condition may be used to set properties based on the operating system and the hardware architecture. Targets may be conditionally executed using the if and unless attributes.
<?xml version="1.0" encoding="UTF-8"?>
<project name="build" basedir="." default="BUILD.XML-COMPILE">
<condition property="Win32-x86">
<and>
<os family="windows" />
<or>
<os arch="i386" />
<os arch="x86" />
</or>
</and>
</condition>
<condition property="Win-ARMv4">
<os family="windows" arch="armv4" />
</condition>
<target name="-BUILD.XML-COMPILE_Win-ARMv4" if="Win-ARMv4"
depends="-init, -pre-compile">
<exec executable="cmd">
<arg value="/c"/>
<arg line='"${g.path.project}\build-wm6-armv4i.bat"'/>
</exec>
<antcall target="-post-compile" inheritall="true" inheritrefs="true" />
</target>
<target name="-BUILD.XML-COMPILE_Win32-x86" if="Win32-x86"
depends="-init, -pre-compile">
<exec executable="cmd">
<arg value="/c"/>
<arg line='"${g.path.project}\build-win32-x86.bat"'/>
</exec>
<antcall target="-post-compile" inheritall="true" inheritrefs="true" />
</target>
<!-- Execute the correct target automatically based on current platform. -->
<target name="BUILD.XML-COMPILE"
depends="-BUILD.XML-COMPILE_Win-ARMv4,
-BUILD.XML-COMPILE_Win32-x86" />
</project>
The paths to the batch files are both single and double quoted so that file paths with spaces will not break the script. I have not tested this script on Windows Mobile 6 ARMV4I, so you will want to use the Ant target below to verify the name.
<target name="echo-os-name-arch-version">
<echo message="OS Name is: ${os.name}" />
<echo message="OS Architecture is: ${os.arch}" />
<echo message="OS Version is: ${os.version}" />
</target>
Related stack overflow questions:
how to detect the windows OS in ANT
Using ant to detect os and set property

Ant build to fail but always execute a target in Jenkins

[Solved] - The correct ant contrib jar was not getting picked up from the default location on my system. Have to give path manually in the build xml like this:
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="/usr/share/ant/lib/ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>
-------------------------------------------
Q: I have a python script that is run through ant/Jenkins like this:
<project name="prjName">
<target name="preRun" description="do something">
....
</target>
<target name="Run" description="Run the python script">
<exec executable="python" failonerror="true">
<arg value="${basedir}/run.py" />
<arg value="something" />
</exec>
</target>
<target name="other1" description="do something">
....
</target>
<target name="other2" description="do something">
....
</target>
</project>
Now this python script runs an external tool (web-inject which produces some result files) and keeps on scanning for the word FAIL in the logs. As soon as it finds FAIL, it does sys.exit("Error")
Thus the build fails but I still want to execute the target - other1. Is it possible through try-catch? I am doing it like this but it isn't working
<macrodef name="test-case">
<sequential>
<trycatch>
<try>
<exec executable="python" failonerror="true">
<arg value="${basedir}/read.py" />
</exec>
</try>
<catch>
<echo>Investigate exceptions in the run!</echo>
</catch>
<finally>
<antcall target="other1" />
</finally>
</trycatch>
</sequential>
</macrodef>
<target name="other1" description="do something">
....
</target>

Running a BAT file from ANT

I have gone through number of posts on the very forum but couldn't sort it out. I am trying to run a BAT file from ANT script. The folder hierarchy is like this
- Project
| - build.xml
| - build-C
| | - test.bat
The ANT file that i wrote so for is
<project name="MyProject" basedir=".">
<property name="buildC" value="${basedire}\build-C" />
<exec dir="${buildC}" executable="cmd" os="Windows XP">
<arg line="/c test.bat"/>
</exec>
</project>
The bat file content is
echo In Build-C Test.bat
It says that build failed .. :s i dun know what wrong am i doing ?
<property name="buildC" value="${basedire}\build-C" />
This should be ${basedir} I guess? Use
<echo>${buildC}</echo>
to make sure the dir is correct.
And shouldn't
<exec dir="${buildC}" executable="test.bat" os="Windows XP" />
do the job?
Hopefully this will help expand on the already given/accepted answers:
I suggest executing cmd with the batch script as a parameter:
<exec failonerror="true" executable="cmd" dir="${buildC}">
<arg line="/c "${buildC}/test.bat""/>
</exec>
Not sure if it is necessary to use the absolute path "${buildC}/test.bat" since dir is specified, but I put it just in case. It might be enough to use /c test.bat.
My project executes a batch script on Windows operating systems & a shell script on all others. Here is an example:
<target name="foo">
<!-- properties for Windows OSes -->
<condition property="script.exec" value="cmd">
<os family="windows"/>
</condition>
<condition property="script.param" value="/c "${basedir}/foo.bat"">
<os family="windows"/>
</condition>
<!-- properties for non-Windows OSes -->
<property name="script.exec" value="sh"/>
<property name="script.param" value=""${basedir}/foo.sh""/>
<echo message="Executing command: ${script.exec} ${script.param}"/>
<exec failonerror="true" executable="${script.exec}" dir="${basedir}">
<arg line="${script.param}"/>
</exec>
</target>

Does grails have an `ant -s` analog?

I'd like to be able to run grails from anywhere in the directory tree.
If there is no such option, you could create a bugreport to introduce such a parameter.
As a workaround, you could create a build.xml wrapping the grails commands in your projects and build your project using this wrapper. Griffon did auto-generate such a wrapper build.xml in early versions. It could look like
<project name="Foo" default="test">
<!-- =================================
target: clean
================================= -->
<target name="clean" description="--> Cleans a Grails application">
<grails>
<arg value="clean"/>
</grails>
</target>
<!-- ... -->
<!-- set up the grails macro -->
<property environment="env"/>
<property name="grails.home" value="${env.GRAILS_HOME}"/>
<property name="jdk.home" value="${env.JAVA_HOME}"/>
<condition property="grails" value="griffon.bat">
<os family="windows"/>
</condition>
<property name="grails" value="grails" />
<macrodef name="grails">
<element name="grails-args" implicit="yes"/>
<sequential>
<exec executable="${grails.home}/bin/${grails}" failonerror="true">
<env key="JAVA_HOME" value="${jdk.home}"/>
<env key="GRAILS_HOME" value="${grails.home}"/>
<grails-args/>
</exec>
</sequential>
</macrodef>
<!-- end set up the grails macro -->
</project>

Resources