Test Creation Workflow Using RestFixture

New to fitness, I want to test Rest APIS with RestFixture, but did not know where to start.

Can someone give me a step-by-step workflow and a sample solution table for GET, POST, and DELETE requests.

we use Json answers, how can I enter them into decision tables when publishing?

thank

+4
source share
1 answer

This answer provides a cookbook for installing RestFixture and a section for introducing a framework adoption program.

Installation

, RestFixture, - RestFixtureLiveDoc. RestFixture Maven. , .

  • JDK ( java -version)
  • maven ( mvn --version)
  • . , , .

Cookbook

  • Maven ( ). :

    mvn archetype:generate -DgroupId=smartrics.restfixture.sample -DartifactId=scratch -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

    • ,
    • src/test/java/smartrics/restfixture/sample/*.java src/main/java/smartrics/restfixture/sample/*.java
  • cd scratch ( , ) pom.xml

  • RestFixture ( RestFixture ). :
 
<dependency>
        <groupId>smartrics.restfixture</groupId>
        <artifactId>smartrics-RestFixture</artifactId>
        <version>4.1</version>
</dependency>
  1. Maven ( , )
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <configuration>
                <outputDirectory>${project.build.directory}/dependencies</outputDirectory>
            </configuration>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
        </execution>
    </executions>
</plugin>
  1. mvn package / ( target/dependencies)
  2. FitNesse ( ),
<dependency>
    <groupId>org.fitnesse</groupId>
    <artifactId>fitnesse</artifactId>
    <version>20151230</version>
</dependency>
  1. mvn clean package
  2. start.bat , ( Linux start.sh):
setLocal EnableDelayedExpansion
set CLASSPATH="
for /R target\dependencies %%a in (*.jar) do (
  set CLASSPATH=!CLASSPATH!;%%a
)
set CLASSPATH=!CLASSPATH!"
java -cp !CLASSPATH! fitnesseMain.FitNesseMain -p 9090 -d src\main\resources -e 0 
  1. FitNesse start.bat ( FitNesse)
    • http://localhost:9090, , .
    • FitNesse CTRL-C
  2. , . git, :
target/
src/main/resources/FitNesseRoot/files/testResults
src/main/resources/FitNesseRoot/ErrorLogs
src/main/resources/FitNesseRoot/RecentChanges
src/main/resources/FitNesseRoot/properties
src/main/resources/FitNesseRoot/updateDoNotCopyOverList
src/main/resources/FitNesseRoot/updateList
src/main/resources/FitNesseRoot/Fitnesse/UserGuide
src/main/resources/FitNesseRoot/PageFooter
src/main/resources/FitNesseRoot/PageHeader
src/main/resources/FitNesseRoot/TemplateLibrary
  1. FitNesse .

    • wiki MyTestSamples - , .
    • MyTestSamples[?], .
    • ,

    !define TEST_SYSTEM {slim} !path target/dependencies/*.jar

    RestFixture

    • .
  2. MyTestSamples .
    • MySample1,
  3. MySample1, .
| Import |
| smartrics.rest.fitnesse.fixture |

|Table: Rest Fixture | http://jsonplaceholder.typicode.com/ |
| GET |/users | 200 | Content-Type : application/json; charset=utf-8 | jsonbody.length == 10|
  1. , Test , .

, , RestFixture ( bin.zip ). , Maven .

RestFixture slf4j, . , :

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.20</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.20</version>
</dependency>

src\main\resources\log4j.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
    <appender name="FILE" class="org.apache.log4j.FileAppender">
        <param name="file" value="restfixture.log" />
        <param name="append" value="false" />
        <param name="threshold" value="debug" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{ISO8601} %-5p [%c{1}] - %m%n" />
        </layout>
    </appender>
    <category name="smartrics.rest.client" additivity="false">
        <priority value="INFO" />
        <appender-ref ref="FILE" />
    </category>
    <category name="smartrics.rest.fitnesse.fixture" additivity="false">
        <priority value="DEBUG" />
        <appender-ref ref="FILE" />
    </category>
    <category name="httpclient.wire" additivity="false">
        <priority value="ERROR" />
        <appender-ref ref="FILE" />
    </category>
    <root>
        <priority value="ERROR" />
        <appender-ref ref="FILE" />
    </root>
</log4j:configuration>

httpclient.wire DEBUG HTTP- .

FitNesse ( )

JUnit runner:

FitNesse JUnit, , Maven Surefire.

src/test/java/smartrics/restfixture/sample/RunnerTest.java

package smartrics.restfixture.sample;

import java.io.File;
import org.junit.Before;
import org.junit.Test;
import fitnesse.junit.JUnitHelper;

public class RunnerTest {

    private JUnitHelper helper;

    @Before
    public void prepare() {
        helper = new JUnitHelper("src/main/resources", new File("target", "fitnesse-junit").getAbsolutePath());
        helper.setPort(9090);
    }

    @Test
    public void restFixtureTests() throws Exception {
        helper.assertSuitePasses("MyTestSamples.MySample1");
    }
}

surefire pom:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.17</version>
    <configuration>
        <forkMode>always</forkMode>
    </configuration>
</plugin>

pom.xml mvn clean package, . , /target/fitnesse-junit/MyTestSamples.MySample1.html

  • pom.xml
  • start.bat

RestFixture , REST API, . Wiki .

RestFixture .

, API, HTTP-/: VERB, URI, / BODY, HEADER / RETURN.

RestFixture , FitNesse:

| Table: Rest Fixture | <base uri> |
| setBody | <body> |
| setHeaders | <headers>
| VERB | /URI | <expected return status code> | <expected response headers | <expected response body> |

<expected ...> . , .

setHeaders setBody , (POST, PUT,...)

, let, HTTP .

(RestFixtureLiveDoc) 1 , RestFixture, - - .

RestFixtureConfig

(RestFixtureConfig) 11 - RestFixture. RestFixture.

|Table: Rest Fixture Config | my_config |
| name1 | value1 | 
| name2 | value2 |

|Table: Rest Fixture | http://host:8080/path | my_config |
| ... |

HTTP: GET, POST, PUT, DELETE, HEAD, OPTION, TRACE. :

| Table: Rest Fixture |http://host:8080 |
|GET    | /resources/0 | 200 | Content-Type: application/json | jsonbody.name === 'Ted' |

| Table: Rest Fixture |http://host:8080/path |
|GET    | /resources/0 | 200 | Content-Type: application/xml | //name[text()='Ted'] |

, (3-, 4- 5- ), ​​ .

, , , REST api.

let .

| Table: Rest Fixture |http://host:8080/path |
|GET    | /resources/0 | 200 | Content-Type: application/json | jsonbody.name === 'Ted' |
| let   |  name | js | response.jsonbody.name |  |

name json response.jsonbody.name. response - json-, HTTP-, jsonbody - , JSON .

:

| setBody | { 'some_name' : '%name%' } |

| script | another fixture | 
| check | not null | $name |

, ( ).

| Table: Rest Fixture  |  http://host:port | 
| comment | an order is created ... |
| POST | /orders        | | |  |
| let  | orderIdUri     | header | Location: (.+) | |
| GET  | %orderIdUri    | | |  |
| let  | email          | js | response.jsonbody.address.email    |
| setBody |!- { 
    'payload' : {
       'message' : 'order sent',
       'email' : '%email%
     }
-!|
| comment | and a confirmation can be sent |
| POST | /notifications | 201 | |  |

API .

RestFixture . :

  • Java: Java Restore / . (RestFixtureExtensions) 12.
  • Javascript:

Javascript RestFixture .

| Table: Rest Fixture Config |  imp |
| restfixture.javascript.imports.map | !- sampleJs=src/main/resources/FitNesseRoot/files/javascript/sample.js -|

- - Javascript , , :

| Table: Rest Fixture |http://host:port | imp |
| GET   | /resources/1.json | 200 |  |  |      
| let   | sum | js | var sum = sampleAdd(1, 2); sum;| |
+12

All Articles