天天看點

nGrinder 的 Groovy 腳本使用指南(Groovy maven 結構)

nGrinder 架構簡介

nGrinder 的 Groovy 腳本使用指南(Groovy 腳本結構)

nGrinder 的 Groovy 腳本使用指南(Groovy maven 結構)

nGrinder 的 Groovy 腳本使用指南(導入 Groovy Maven 工程到 IntelliJ)

當你建立腳本時,如果你選擇使用 Groovy 腳本,除了 JUnit 方式測試外,它将類似于 Jython 腳本那樣執行。如果你不僅想使用 Groovy 而且還要在 IDE 中執行 JUnit,同時又能簡單的配置依賴,你可以選擇先建立一個 Groovy 的 Maven 項目。

當你第一次運作 Groovy 的 Maven 項目時,可能需要一些時間運作測試。
這是因為 Maven 項目需要從 Maven倉庫下載下傳相關的庫。請耐心等待。::)。
           

登陸到 nGrinder, 點選 “腳本” ==> “建立腳本”,選擇 “Groovy Maven Project” 類别

nGrinder 的 Groovy 腳本使用指南(Groovy maven 結構)

寫入腳本名稱,和被測試的 URL 位址,選擇 “建立資源和庫目錄”(可選),然後點選“建立”按鈕,即可在 nGrinder 上成功建立一個 Groovy 的 Maven 工程。

nGrinder 的 Groovy 腳本使用指南(Groovy maven 結構)

工程對應的 SVN 位址如圖中所示,“http://xxxx:xxx/svn/admin/test” ,“admin” 使用使用者名,“test”是工程名,工程目錄結構如下:

目錄 描述
${name}/pom.xml Maven 的工程檔案
${name}/src/main/java/TestRunner.groovy 預設的 Groovy 腳本檔案
${name}/src/main/java/resources1.txt 預設的資源檔案
${name}/lib 存放私有庫

你可以使用 SVN 的方式将項目導入到你的 IDE

參考:nGrinder 的 Groovy 腳本使用指南(導入 Groovy Maven 工程到 IntelliJ)

在 pom.xml 中,你可以指定額外的依賴庫

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>ngrinder</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1</version>

    <properties>
        <ngrinder.version>3.4</ngrinder.version>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <repositories>
        <repository>
            <id>ngrinder.maven.repo</id>
            <url>https://github.com/naver/ngrinder/raw/ngrinder.maven.repo/releases</url>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>org.ngrinder</groupId>
            <artifactId>ngrinder-groovy</artifactId>
            <version>${ngrinder.version}</version>
            <scope>provided</scope>
        </dependency>

        <!-- 添加依賴 -->
        <!--
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.0.1</version>
        </dependency>
         -->
        <!-- 添加你的私有庫依賴 -->
        <!--
        <dependency>
            <groupId>your_lib</groupId>
            <artifactId>your_lib</artifactId>
            <version>your_lib_version</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/hello.jar</systemPath>
        </dependency>
        -->
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <additionalProjectnatures>
                        <projectnature>
                            org.eclipse.jdt.groovy.core.groovyNature
                        </projectnature>
                        <projectnature>
                            org.eclipse.m2e.core.maven2Nature
                        </projectnature>
                    </additionalProjectnatures>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
           

當一個腳本在性能測試配置頁被選中時,controller 會自動監測此腳本是否在 ${name}/src/main/java 檔案夾下,同時監測 pom.xml 是否在基礎檔案夾(相對于目前 Groovy 工程)下。如果 controller 檢測到了它們,在分發給代理之前 controller 将會遞歸的拷貝在 ${name}/src/main/resources 和 ${name}/src/main/java 目錄下的檔案到準備分發目錄下。下圖說明了 SVN 中的每個檔案夾是如何拷貝到準備分發目錄的

- from svn foler + pom.xml
                 + src/main/java/Test1.groovy
                                /package_names/the_other_groovyscripts
                 + src/main/resources/resources1.txt
                                     /subfolder_names/the_other_resources
                 + lib (private libraries)


- to dist folder + Test1.groovy 
                 + resources1.txt 
                 + package_names/the_other_groovy_scripts 
                 + subfolder_names/the_other_resources 
                 + lib (copied from maven dependencies and files in lib folder existing subversion)
           

在普通的 Jython 腳本和 Groovy 腳本中, 可以使用 “open(“./resources/resource1.txt”)” 或者 “new File(“./resources/resource1.txt”)加載資源檔案。然而在 Groovy 的 Maven 工程中将不可用。

我們不得不替換此路徑到 classpath 的基礎資源路徑,確定可以在 IDE 中可以運作 Groovy 的 JUnit 測試用例,資源檔案在分發拷貝時将會保持目錄層次。你可以使用下面的代碼加載資源檔案

import org.codehaus.groovy.reflection.ReflectionUtils;
....


class YourTest {
    String text;

    @BeforeThread
    public void beforeThread() {
       // In groovy, InputStream contains text field.
       text = loadResourceFromClassPath("/resource1.txt").text;
    }

    @Test
    public void doTest() {
       ....
    }

    // This is groovy way to load resource from classpath
    public loadResourceFromClassPath(String resourcePath) {
        return ReflectionUtils.getCallingClass().getResourceAsStream(resourcePath);
    }
}
           

原文位址:http://www.cubrid.org/wiki_ngrinder/entry/groovy-maven-structure