Movatterモバイル変換


[0]ホーム

URL:


Test Anything Protocol


Testing with Java

Testing using tap4j

Installation

In order to install tap4j you have to download a jar fromhttps://tupilabs.com/tap4j/tap4j or if you are a Maven user you can add a dependency in your pom.xml, as shown below.

<dependency><groupid>org.tap4j</groupid><artifactid>tap4j</artifactid><version>${tap4j.version}</version></dependency>

Differently than in others implementations, tap4j uses an Object Oriented approach. Instead of calling methods like ok(), not_ok(), is() and isnt(), you construct objects such as a TAP Producer to dump TAP and a TAP Consumer to load TAP. ATestSet object is used to encapsulate all the TAP Elements.

Generating TAP using a TAP Producer

The TAP Producers in tap4j are created using aTapProducerFactory. After a TAP Producer is created we can call the dump method passing aTestSet to have the result TAP.

TapProducertapProducer=TapProducerFactory.makeTap13Producer();testSettestSet=newtestSet();// Creating a Plan with 2 test Result'sPlanplan=newPlan(2);testSet.setPlan(plan);// Creating a test Result with a status oktestResulttr1=newtestResult(StatusValues.OK,1);testSet.addtestResult(tr1);// Creating a test Result with status not_oktestResulttr2=newtestResult(StatusValues.NOT_OK,2);testSet.addtestResult(tr2);// Retrieving the TAP content and printing it to the default outputStringtapStream=tapProducer.dump(testSet);System.out.println(tapStream);

The code above will generate the following TAP output:

1..2ok 1not ok 2

Reading TAP using a TAP Consumer

If you understood about how tap4j works, reading a TAP Stream will be quite easy for you. You just have to create a TAP Consumer using guess what? Yeah, aTapConsumerFactory. The method of the TAP Consumer to load TAP is load(). This method will return aTestSet. Let’s read the output from the previous section.

TapConsumertapConsumer=TapConsumerFactory.makeTap13Consumer();StringtapStream="1..2\n"+"ok 1\n"+"not ok 2";testSettestSet=tapConsumer.load(tapStream);System.out.println(testSet.getNumberOftestResults());System.out.println(testSet.gettestResult(2).getStatus());System.out.println(testSet.containsNotOk());

The code above will generate the following output:

2not oktrue

github.com/testanything


[8]ページ先頭

©2009-2025 Movatter.jp