Testing with JUnit Flashcards

1
Q

Explain junit task

A

<path id=”test.classpath”>
<fileset dir=”lib”>
<include name=”**/*.jar” />
</fileset>
<pathelement location=”build/classes” />
</path>

<target name=”compile-test” depends=”compile” description=”compiles test files”>
<javac srcdir=”test” destdir=”build/classes” debug=”true” verbose=”true” deprecation=”true” classpathref=”test.classpath” />
<echo>test classes compilation complete!</echo>
</target>

<target name=”test” description=”runs junit tests” depends=”compile-test”>
<junit fork=”ture” failureproperty=”test.failure” showoutput=”yes” includeantruntime=”false”>
<classpath refid=”test.classpath” />
<test name=”com.antdemo.DemoTest” />
</junit>
<fail message=”Unit test failed” if=”test.failure” />
</target>

turnoff include runtime unless you are writing a custom ant task

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the different formatters available

A

brief Provides details of test failures in text format.
plain Provides details of test failures and statistics of each test run in text format.
xml Provides an extensive amount of detail in XML format including Ant’s properties
at the time of testing, system out, and system error output of each test case.

<junit printsummary=”false” haltonfailure=”true”>
<classpath refid=”test.classpath”/>
<formatter type=”brief” usefile=”false”/>
<test name=”org.example.antbook.ant.lucene.HtmlDocumentTest”/>
</junit>

<formatter type=”xml”/>
<test todir=”${test.data.dir}”
name=”org.example.antbook.ant.lucene.HtmlDocumentTest”/>

You can create cutom formatter by using classname property rather than type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Viewing System.out and System.err output

A

Using printsummary on junit task

printsummary=”withOutAndErr”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to run multiple tests

A

<junit printsummary=”true” haltonfailure=”true”>
<classpath refid=”test.classpath”/>
<formatter type=”brief” usefile=”false”/>
<formatter type=”xml”/>
<batchtest todir=”${test.data.dir}”>
<fileset dir=”${test.dir}” includes=”**/*Test.class”/>
</batchtest>
</junit>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to generate junit reports?

A

<target name=”test” description=”runs junit tests” depends=”compile-test”>
<junit fork=”ture” errorproperty=”test.failure” failureproperty=”test.failure”
showoutput=”yes” includeantruntime=”false” printsummary=”false”>
<classpath refid=”test.classpath” />
<formatter type=”brief” usefile=”false” />
<formatter type=”xml” />
<batchtest todir=”${test.data.dir}”>
<fileset dir=”${test.dir}” includes=”**/*Test.class” />
</batchtest>
</junit>
<junitreport todir=”${test.data.dir}”>
<fileset dir=”${test.data.dir}” includes=”TEST-*.xml” />
<report format=”frames” todir=”${test.data.dir}” />
</junitreport>
<fail message=”Unit test failed” if=”test.failure” />
</target>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Run a single test case from the command-line

A

<junit printsummary=”false”
errorProperty=”test.failed”
failureProperty=”test.failed”>
<classpath refid=”test.classpath”/>
<formatter type=”brief” usefile=”false”/>
<formatter type=”xml”/>
<test name=”${testcase}” todir=”${test.data.dir}” if=”testcase”/>
<batchtest todir=”${test.data.dir}” unless=”testcase”>
<fileset dir=”${test.dir}” includes=”**/*Test.class”/>
</batchtest>
</junit>
By default, testcase will not be defined, the <test> will be ignored, and
<batchtest> will execute all of the test cases. In order to run a single test case, run
Ant using a command line like
ant test -Dtestcase=<fully qualified classname>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

List out best practices with forking

A

Forking allows you to run the code in different JVM.

Inorder to use the same classpath the test classpath should include java.class.path (this is JVM property)

<classpath>
<path refid=”test.classpath”/>
<pathelement path=”${java.class.path}”/>
</classpath>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Configuring testcases dynamically

A

The nested <sysproperty> element of <junit> provides a system property to
the executing test cases, the equivalent of a -D argument to a Java command-line program:
<junit printsummary=”false”
errorProperty=”test.failed”
failureProperty=”test.failed”>
<classpath refid=”test.classpath”/>
<sysproperty key=”docs.dir” value=”${test.dir}/org”/>
<sysproperty key=”index.dir” value=”${test.dir}/index”/>
<formatter type=”xml”/>
<formatter type=”brief” usefile=”false”/>
<test name=”${testcase}” if=”testcase”/>
<batchtest todir=”${test.data.dir}” unless=”testcase”>
<fileset dir=”${test.dir}” includes=”**/*Test.class”/>
</batchtest>
</junit>

private String docsDir = System.getProperty(“docs.dir”);
private String indexDir = System.getProperty(“index.dir”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

SQL task

A

Ant’s <sql> task can preconfigure a database with known test data prior to running
unit tests. The DBUnit framework (http://dbunit.sourceforge.net/) is also a
handy way to ensure known database state for test cases.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to enable short circuit testing

A

<target name=”test” description=”runs junit tests” depends=”compile-test” unless=”tests.uptodate”>
<junit fork=”ture” haltonfailure=”false” errorproperty=”test.failure” failureproperty=”test.failure” showoutput=”yes” includeantruntime=”false” printsummary=”false”>
<classpath refid=”test.classpath” />
<formatter type=”brief” usefile=”false” />
<formatter type=”xml” />
<batchtest todir=”${test.data.dir}”>
<fileset dir=”${classes.dir}” includes=”**/*Test.class” />
</batchtest>
</junit>
<junitreport todir=”${test.data.dir}”>
<fileset dir=”${test.data.dir}” includes=”TEST-*.xml” />
<report format=”frames” todir=”${test.data.dir}” />
</junitreport>

<echo file=”${test.last.failed.file}” message=”Tests failed” />
<fail message=”Unit test failed” if=”test.failure” />
<delete file=”${test.last.failed.file}” />
</target>

<condition property=”tests.uptodate”>
<and>
<uptodate>
<srcfiles dir=”${src.dir}” includes=”**/*.java” />
<mapper type=”glob” from=”*.java” to=”${classes.dir}/*.class” />
</uptodate>
<uptodate>
<srcfiles dir=”${test.src.dir}” includes=”**/*.java” />
<mapper type=”glob” from=”*.java” to=”${classes.dir}/*.class” />
</uptodate>
<uptodate>
<srcfiles dir=”${test.src.dir}” excludes=”**/*.java” />
<mapper type=”glob” from=”*” to=”${classes.dir}/*” />
</uptodate>
<not>
<available file=”${test.last.failed.file}” />
</not>
<!–not>
<isset property=”testcase” />
</not–>
<uptodate>
<srcfiles dir=”${test.src.dir}” includes=”**/*.java” />
<mapper type=”package” from=”*Test.java” to=”${test.data.dir}/TEST-*Test.xml” />
</uptodate>
</and>
</condition>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

best practicies

A

Clear previous test results before running new tests; delete and recreate the test
results and reports directories.
• Set haltonfailure=”false” on <junit> to allow reporting or other
steps to occur before the build fails. Capture the failure/error status in a single
Ant property using errorProperty and failureProperty.
• Pick a unique naming convention for test cases: *Test.java. Then you can use
<batchtest> with Ant’s pattern matching facility to run only the files that
match the naming convention. This helps you avoid attempting to run helper
or base classes.
• Separate test code from production code. Give them each their own unique directory
tree with the same package naming structure. This lets tests live in the same
package as the objects they test, while still keeping them separate during a build.
• Capture results using the XML formatter: <formatter type=”xml”/>.
• Use <junitreport>, which generates fantastic color enhanced reports to
quickly access detailed failure information.
• Fail the build if an error or failure occurred: <fail if=”test.failed”/>.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly