Testing of program modules using JUNIT4, regression testing

Source: Internet
Author: User

① to import JUNIT4 's package into your project.

② Right-click to create the JUnit test class and write the test code in the test class.

There are a few things that JUnit needs to know at the moment:

Fixture Series: Beforeclass,afterclass,before,after

General test: Ignore (Ignore), Text (test), test (timeout = 1000) (timed test), test (expected = Arithmeticexception.class) (Exception test)

Special tests: Batch parameter testing, package testing

③fixture series and common test example code

[Java] View plain

Copy

1. Package com.mikuscallion.main;

2.

3. import static org.junit.assert.*;

4.

5. Import Org.junit.After;

6. Import Org.junit.AfterClass;

7. Import Org.junit.Before;

8. Import Org.junit.BeforeClass;

9. Import Org.junit.Ignore;

. import Org.junit.Test;

One. public class Calculatortest {

12.//Test Object

Calculator Calculator =new Calculator ();

//fixture.

@BeforeClass.

public static void Beforeclass () {

System.out.println ("Call once during test class initialization");

18.}

@AfterClass

public static void Afterclass () {

System.out.println ("Call once at the end of the test class run");

22.}

@Before.

public void before () {

System.out.println ("Call before executing any test code");

Calculator.clear ();

27.}

@After.

public void after () {

System.out.println ("Call after executing any test code");

31.}

//test---------------------------------------------------------------------------------

@Ignore.

34.//Neglect Testing

public void testmultiply () {

Calculator.multiply (1);

Panax calculator.multiply (5);

38.//Assertion Result

Assertequals (5, Calculator.getresult ());

40.}

@Test (timeout = 1000)

42.//Time-Limit test

public void Limittimetest () {

Calculator.squareroot (5);

45.}

@Test (expected = arithmeticexception.class)

47.//Anomaly Test

. public void Excepttest () {

Calculator.divide (0);

50.}

Wuyi @Test.

Testadd public void () {

Calculator.add (1);

Calculator.add (2);

Assertequals (3, Calculator.getresult ());

56.}

@Test.

. public void Testsubstract () {

Calculator.add (10);

Calculator.substract (2);

Assertequals (8, Calculator.getresult ());

62.}

@Test.

Testdivide public void () {

Calculator.add (8);

Calculator.divide (2);

Assertequals (4, Calculator.getresult ());

68.}

69.

70.}

④ parameter Test code

[Java] View plain

Copy

1. Package com.mikuscallion.main;

2.

3. import static org.junit.assert.*;

4.

5. Import Java.util.Arrays;

6. Import Java.util.Collection;

7.

8. Import Org.junit.Test;

9. Import Org.junit.runner.RunWith;

. import org.junit.runners.Parameterized;

One. Import org.junit.runners.Parameterized.Parameters;

12.

@RunWith (Parameterized.class)

public class Squaretest {

. public Calculator Calculator =new Calculator ();

. public int param;

. public int result;

18.

@Parameters

public static Collection data () {

21.//Pay attention to this notation

Return Arrays.aslist (New object[][]{

23. {2, 4},

24. {0, 0},

25. {-3, 9},

26.});

27.}

. public squaretest (int param, int result) {

This.param = param;

This.result = result;

31.}

@Test.

. public void Testsquare () {

Calculator.square (param);

Assertequals (result, Calculator.getresult ());

36.}

37.}

⑤ Batch Test code

[Java] View plain

Copy

1. Package com.mikuscallion.main;

2.

3.

4. Import Org.junit.runner.RunWith;

5. Import Org.junit.runners.Suite;

6. Import org.junit.runners.Suite.SuiteClasses;

7. @SuiteClasses ({

8. Squaretest.class,

9. Calculatortest.class,

10.})

@RunWith (Suite.class)

public class Suitetest {

13.

14.}

⑥ the class code to be tested

[Java] View plain

Copy

1. Package com.mikuscallion.main;

2. public class Calculator {

3.

4. private static int result; Static variables for storing run results

5.

6. public void Add (int n) {

7. Result = result + N;

8.}

9. public void substract (int n) {

Ten. result = Result-n; Bug: The correct one should be result =result-n

11.}

public void Multiply (int n) {

13.}//This method has not been written well

public void divide (int n) {

result = result/n;

16.}

17.

18.//

N. public void Square (int n) {

result = n * n;

21.}

public void SquareRoot (int n) {

for (;;);//bug: Dead Loop

24.}

public void Clear () {//zeroing the results

result = 0;

27.}

. public int GetResult () {

return result;

30.}

31.}

GitHub Link: https://github.com/SuNNed/Junit4/blob/master/test.md

Testing of program modules using JUNIT4, regression testing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.