Introduction to JUnit and Application (Traditional)

xiaoxiao2021-03-06  57

Introduction and Application of JUnit

Horance Chou (hornce@freera.net)

I. Introduction:

The purpose of "test" is to find the difference between "product" and "design". In the field of software development, testing is important to verify software features. There are many ways to test, but the most basic and intuitional tests are "Unit Test" (Unit Test) that is performed by program developers when writing program components. In today's various software development methods, the efficacy of unit tests is gradually attached; in recent years, the Path Program Design Law * (XP) [1] and "Test Guidance Development Method" (Test "in recent years -Driven development, hereinafter referred to as TDD) [2], unit tests are also listed as one of the main points of this two methodology. This article describes how to write test cases (Test Case) using the unit test platform-JUnit [3] that uses Java program languages ​​to speed up the test case to accelerate the development / test loop (Developing / Testing Item).

* Note: This is a noun that Tang Zonghan (AUTRIJUS TAN) is used in TAIPEI.PM gatherings Extreme Programming, and personal feels very adex, which is used in this translation.

Two. JUnit Introduction

JUnit is the regression test platform developed by Erich Gamma and Kent Beck. As the name suggests, this platform is to provide a Java program developer's actual unit test case related API (Application Programming Interfaces, Application Interface). Junit is a free software (Open Source Software) [4]. JUnit's authorization method is the CommON Public License version 1.0. The latest stable version is 3.8.1, which can be taken from SourceForge [5] to get the latest release of this platform.

3. Why use JUnit

Whether the item you participate in follows XP or TDD, the unit test is inevitable. In the rules and practices defined by XP, the unit test has a large proportion of large specific gravity [6]. Due to the JAVA object-oriented language characteristics, and we can at least get the following benefits:

1. No need to write a remode code for unit testing:

With JUnit, developers can easily establish test cases to test methods in existing items. Writing a unit test case using the JUnit platform is as simple as writing some methods composed of 5 ~ 10 trip codes - in fact, if you match some development tools, you don't even need to write any program code. We will prove this in the instance later.

2. JUnit test cases can be organized into test combinations (TEST SUITES):

JUnit's test combination can be admitted to several test cases or other test portfolios, so that developers can complete the test of related components in a test action.

3. JUnit's test results are easy to collect:

A basic test execution environment is provided in the JUnit kit, such as junit.textui.teStrunner executed in text mode, and junit.swingui.teestrunner executed under the Type Interface (GUI), both help developers simple Execute the completed test combination or test case.

Finally, it is also the most important point: JUnit is free software.

IV. How to use JUnit

1. Install JUnit:

You can get JUnit framework in any SourceFor Site, such as: http://umn.dl.sourceforge.net/sourceForge/junit/junit3.8.1.zip

After JUnit3.8.1.zip, you can use the usual decompression tool to unlock JUnit3.8.1.zip to the specified directory. If you want to use JUnit Framework directly in your Java execution environment, you can add JUnit.jar at the ClassPath environment variable, refer to the environment variable setting method under various job systems.

2. Example: Moneytest

Public static void main (String [] args) {

Money M12CHF = New Money (12, "CHF");

Money M14CHF = New Money (14, "CHF");

Money RES = M12CHF.Add (M14CHF);

System.out.println ("res:" res. activity () " res.currency ());

}

Next, you open a command line window, with Javac Compile, your Money.java

Java junit.samples.money

To perform this program, then praying the output result:

Res: 26 CHF

Such a way of writing is the most common way of many program developers, but in this way, you must perform normal in manual judgment. Now let's take a look at how to use JUnit to create a test case:

In the JUnit 3.8.1.zip you have acquired contain a simple JUnit example, you can find the original code of this sample in the junit / samples directory. Now, you want to write a few test cases to test the add () method in this Money object. Recall that you have done such actions: add such a program code in the Money object: (Note: CHF is the currency abbreviation for SWSS FRANCS, where the original usage in the JUnit paradix is ​​reserved)

1. Write a Moneytest and inherit junit.framework.testcase (please refer to junit / samples / moneytest.java)

2. Write a TestsImpleAdd () Method, the content is:

Public void testsimpleadd () {

Money M12CHF = New Money (12, "CHF");

Money M14CHF = New Money (14, "CHF");

Money expectedreturn = new Money (26, "chf");

Money actualreturn = m12chf.add (M14CHF);

AssertTrue (ExpectedReturn.equals (actualreturn));

}

It should be noted that when there is no override junit.framework.testcase item, Testrunner will automatically perform all named "Test" as starting, such as TestsimpleAdd, TestsImplesub (if there is Definition), etc.. Asserttrue () in the last line in the TestsImpleAdd () method is the API used to check the test results in the JUnit Framework, and if ExpectedReturn and ActualReturn are equal (with the passage of the equals () method), Junit This test will only be passed. There are many other Assert methods in JUnit Framework, such as Assertequals (), assertNotequals (), etc., please refer to the JUnit API Javadoc file [7]. 3. Execute Testrunner, for example:

Java -cp $ {classpath}: / path / to / junit.jar :.junit.textui.teestrunner junit.samples.money.MoneyTestst

.

Time: 0.01

OK (1 tests)

Here is JUnit.Textui.Testrunner in text model to perform JUnit test. You can see that JUnit has successfully implemented your MoneyTest and reply to "OK (1 Tests)", indicating that Testrunner has been tested and all passed. You can change JUnit.textui.Testrunner in the example to junit.swingui.teStrunner, and observe what is different.

4. If you want to perform several different tests in a test case, do not want to write objects in each test method (ie new Money), you can override Setup () And Teardown () methods to build your Test Configuration (TEST FIXTURES):

Public class mothertest extends testcase {

PRIVATE MONEY F12CHF;

PRIVATE MONEY F14CHF;

PRIVATE MONEY F28USD;

protected void setup () {

F12CHF = New Money (12, "CHF");

F14CHF = New Money (14, "CHF");

F28usd = new Money (28, "USD");

}

}

Testrunner automatically calls setup () and calls the Teardown () method before the end. All test methods in the object directly use these three MONEY objects. Therefore, the pre-example TestsImpleAdd can be rewritten:

Public void testsimpleadd () {

// [12 CHF] [14 CHF] == [26 CHF]

Money expertted = new Money (26, "chf");

Assertequals (Expected, F12chf.Add (F14CHF));

}

5. So how do you do multiple tests in one execution? You can use Test Suite to reach: Add Suite () static method in your MoneyTest category:

Public static test suite () {

Testsuite suite = new testsuite ();

Suite.Addtest (New Moneytest ("TestMoneyEquals");

Suite.Addtest (New Moneytest ("TestsimpleAdd")); Return Suite;

}

As a result, Testrunner will test in the test combination of your defined test. If you don't define Suite () methods for your TestCase category, Testrunner automatically performs all test methods that start with "TEST". The basic usage of JUnit Framework is introduced. If you need more advanced usage, see the reference materials provided in the sixteenth point and extended reading.

Five. Conclusion

JUnit is popular, not just because he is a free software, but a new development methodology - such as XP and TDD began to pay attention, and developers feel the benefits of using Junit, and thus in Java developers It is increasingly used. This article focuses on the installation and basic use of JUnit. If you can match the development methods such as XP and TDD, it should be more likely to Finally, I hope that this article will make you a preliminary understanding of JUnit and integrate into your development process, which will help your future project development work.

Sixth. Reference

Http://www.extremeprogramming.org/ http://www.testdriven.com/ http://www.junit.org/ http://www.opensource.org/ http://sourceforge.net/projects/ JUnit / http://www.extremeprogramming.org/rules.html http://junit.sourceforge.net/javadoc/index.html

Yan Shen reading:

Extreme Programming:. A gentle introduction http://www.extremeprogramming.org/ Zodiac programming method by Autrijus Tang http://aut.dyndns.org/xp/slides/start.html JUnit Documentation: http: // junit. sourceforge.net/#Documentation JUnit Primer http://www.clarkware.com/articles/JUnitPrimer.html Introducing JUnit by Alan Griffiths http://www.octopull.demon.co.uk/java/Introducing_JUnit.html

转载请注明原文地址:https://www.9cbs.com/read-113014.html

New Post(0)