TestNG+Ant自動執行測試
阿新 • • 發佈:2019-01-23
如何在Ant的build.xml裡面正確配置TestNG呢?
1. 在build.xml裡面定義testng任務,在classpath裡面指定testng.jar
<taskdef resource="testngtasks" classpath="${3rd.lib.dir}/testng.jar"/>
2. 在build.xml裡面新建一個 叫regression的target
在target裡面新建一個testng標籤,裡面需要設定的屬性有:outputdir – 測試結果輸出目錄;classpathref – 那些自動化測試程式碼的目標路徑,通常就是編譯完成以後的那個目標路徑,例如xxx/bin;delegateCommandSystemProperties – 接受傳遞命令列引數作為系統變數,這個設定為true可以在呼叫Ant的時候通過 -Dfoo=value 把引數傳遞給TestNG;裡面還有一個xmlfileset節點,這個節點就是指定testng.xml檔案的目錄以及具體檔案。<project basedir="." default="regression" name="automation test"> <property name="base.dir" value="/home/maguschen/workspaces/automation"/> <property name="testng.output.dir" value="${base.dir}/test-output"/> <property name="3rd.lib.dir" value="${base.dir}/libs"/> <property name="testng.file" value="testng.xml"/> <taskdef resource="testngtasks" classpath="${3rd.lib.dir}/testng.jar"/> <target name="clean"> <delete dir="${base.dir}/bin"/> </target> <target name="compile" depends="clean"> <mkdir dir="${base.dir}/bin"/> <javac srcdir="${base.dir}/src" destdir="${base.dir}/bin" classpathref="classes" includeantruntime="off" debug="on" debuglevel="lines,vars,source"/> </target> <path id="classes"> <fileset dir="${3rd.lib.dir}" includes="*jar"/> <fileset dir="${3rd.lib.dir}" includes="*zip"/> <pathelement location="${base.dir}/bin"/> </path> <target name="regression" depends="compile"> <testng outputdir="${testng.output.dir}" classpathref="classes" delegateCommandSystemProperties="true"> <xmlfileset dir="${base.dir}" includes="${testng.file}"/> </testng> </target> </project>
regression 的 target 有一個depends屬性,意思就是跑regression之前需要做compile,而跑compile之前需要clean,應該很容易理解。直接在命令列裡面執行:
@Parameters({"url"}) @Test public void search(String url){ WebDriver driver = new FirefoxDriver(); driver.get(url); WebElement query = driver.findElement(By.name("q")); query.sendKeys("Cheese"); query.submit(); }
如果這樣呼叫:ant -Durl=http://www.google.com -f build regression 。那麼就會進入google的首頁搜尋,如果是: ant -Durl=http://magustest.com -f build regression ,那麼就會找不到叫“q”的元素,呵呵。
接下來只要把cron job配好就完成了
15 * * * * ant -f /home/maguschen/workspaces/automation/build.xml regression