If you are a programmer who is reluctant to write documents, you are not alone. However, when your boss is checking your work, he doesn't want to read your pile of code. He needs to read the document, what you need now is Concordion, an automated testing framework that complies with Specification By Example, which describes software functions using natural language, that is, html documents that all members of the project can understand and have the test function.
(1) how Concordion works
To put it simply, the Concordion test is only an extension of JUnit, but it can read test data from your written test document (html) and run the test through the traditional JUnit, output the test result as an html document with a red-green mark (indicating failure or success) (based on the original test document ).
In, Specification is the html test document we have written. Unlike Common html documents, we need to add some labels named concordion to them, which will be ignored by the browser, however, Concordion uses these labels to execute test commands, such as calling test functions in Fixture. Fixture is a test case inherited from ConcordionTestCase (ultimately inherited from the JUnit test class). These test cases call our own function code.
(2) Concordion's Hello World
The following uses a simple Hello World example to demonstrate Concordion.
Download Concordion and the package on which it depends.
First, write an HTML testing example helloworld.html:
<body>
<p>Should print:</p>
<p concordion:assertEquals="sayHello()">HelloWorld</p>
</body>
This html document can be opened normally through the browser. Because the browser does not know the concordion tag, ignore it:
We can see that the html test document of the concordion tag added above is no different from the ordinary html document. At the same time, we also see a sayHello () function call after the concordion tag, where does this function come from? This is the Concordion Convention, which requires that there is a "HelloWorldTest" in the same directory of the html document. java test case class exists, and this class has a test function named sayHello (). The agreed rule is: if the HTML document is foo.html, the test case class should be FooTest. java. In this case, Concordion finds and calls the sayHello () function named HelloWorldTest by name matching.
We also noticed that there is assertEquals after the concordion tag. In this case, Concordion will compare the output of the helloWorld () function with the subsequent "HelloWorld" string. If it is equal, the test is successful. Otherwise, the test fails.
To download the helloworldtestclass, create the HelloWorldTest. java file in the same directory as helloworld.html:
package com.thoughtworks.davenkin.concordion;
import org.concordion.integration.junit3.ConcordionTestCase;
public class HelloWorldTest extends ConcordionTestCase {
public String sayHello()
{
return new HelloWorld().sayHelloWorld();
}
}
A HelloWorld object is instantiated in the HelloWorldTest. java file and Its sayHelloWorld () method is called. Therefore, a HelloWorld class to be tested is written as follows:
package com.thoughtworks.davenkin.concordion;
public class HelloWorld
{
public String sayHelloWorld()
{
return "HelloWorld";
}
}
The final generated project directory structure is as follows:
(3) Compile and run the test
Open the terminal and switch the directory to concordion (this is the root directory of the project created by the author for helloworld, please do not confuse with the concordion mentioned above). Compile:
javac -cp lib/*:src:test test/com/thoughtworks/davenkin/concordion/HelloWorldTest.java
Run the test with JUnit:
java -cp lib/*:src:test org.junit.runner.JUnitCore com.thoughtworks.davenkin.concordion.HelloWorldTest
The running result is as follows:
JUnit version 4.8.2
./var/folders/wM/wMUC8-0FEsq-MMkTzdzYA++++TI/-Tmp-/concordion/com/thoughtworks/davenkin/concordion/HelloWorld.html
Successes: 1, Failures: 0
Time: 0.307
OK (1 test)
Run successfully, and the full path name of the html file output by the test is displayed (remove the previous point "."):
/var/folders/wM/wMUC8-0FEsq-MMkTzdzYA++++TI/-Tmp-/concordion/com/thoughtworks/davenkin/concordion/HelloWorld.html
Open the file:
The test runs successfully.