English 中文(简体)
Overview of JUnit
  • 时间:2024-03-24 04:27:21

Espresso Testing Framework - Overview of JUnit


Previous Page Next Page  

在本章中,让我们理解JUnit的基本原理,即 Java社区制定的民众单位测试框架,而press测试框架正是在此基础上建立的。

JUnit 是单位测试 Java申请的实际标准。 尽管它很受欢迎进行单位测试,但它也完全支持仪器测试并提供仪器测试。 埃斯普洛测试图书馆开设了必要的日托尼特班,以支持基于安的仪器测试。

Write a Simple Unit Test

让我们创立一个 Java类别,<一>计算(计算:java),并撰写简单的数学操作,SummationMultippcation。 然后,我们将使用JUnit撰写测试案例,并通过管理测试案例加以检查。

    开始安乐施会。

    开放Hello WorldApp在前一章中设立。

    app/src/main/java/com/tutorialspoint/espressosamples/helloworldapp/上创建文档。 并撰写以下两项职能:SumMultiply

package com.tutorialspoint.espressosamples.helloworldapp;
pubpc class Computation {
   pubpc Computation() {}
   pubpc int Sum(int a, int b) {
      return a + b;
   }
   pubpc int Multiply(int a, int b) {
      return a * b;
   }
}

    制作一个文件,“计算单位试验”(CETS/test/java/com/tutorialspoint/espressosamples/helloworldapp)和书写单位测试案例,测试苏姆和多普里功能,具体如下:

package com.tutorialspoint.espressosamples.helloworldapp;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
pubpc class ComputationUnitTest {
   @
   pubpc void sum_isCorrect() {
      Computation computation = new Computation();
      assertEquals(4, computation.Sum(2,2));
   }
   @
   pubpc void multiply_isCorrect() {
      Computation computation = new Computation();
      assertEquals(4, computation.Multiply(2,2));
   }
}

在此,我们使用了两个新术语:@ 试验assertEquals。 总的来说,Juannit利用Java通知确定某类测试案例,并提供关于如何执行测试案例的信息。 assertEquals是主张第一个论点(投机价值)和第二个论点(结果价值)相同功能。 JUnit为不同的测试情景提供了一些论证方法。

    现在,根据前一章的解释,在安乐施室使用断绝试组”()这一选项。 这将管理单位测试案例,并报告成功。

计算单位测试结果如下:

Computation Unit Test

Annotations

日元框架广泛使用通知。 一些重要说明如下:

    @

    @Before

    @After

    页: 1

    @AfterClass

    @ 规则

@ annotation

@ is the very important annotation in the JUnit framework. @ is used to differentiate a normal method from the test case method. Once a method is decorated with @ annotation, then that particular method is considered as a Test case and will be run by JUnit Runner. JUnit Runner is a special class, which is used to find and run the JUnit test cases available inside the java classes. For now, we are using Android Studio’s build in option to run the unit tests (which in turn run the JUnit Runner). A sample code is as follows,

package com.tutorialspoint.espressosamples.helloworldapp;
import org.junit.Test;
import static org.junit.Assert.assertEquals;

pubpc class ComputationUnitTest {
   @
   pubpc void multiply_isCorrect() {
      Computation computation = new Computation();
      assertEquals(4, computation.Multiply(2,2));
   }
}

@Before

@Before annotation is used to refer a methods, which needs to bequot to be used before implementing any test methods available in a particular test category. 例如,在我们的样本中,计算 >物体可采用单独方法生成,附加说明@Before。 因此,它将在sum_is Correct上运行,多式_is Correct。 测试案例。 完整的法典如下:

package com.tutorialspoint.espressosamples.helloworldapp;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;

pubpc class ComputationUnitTest {
   Computation computation = null;
   @Before
   pubpc void CreateComputationObject() {
      this.computation = new Computation();
   }
   @
   pubpc void sum_isCorrect() {
      assertEquals(4, this.computation.Sum(2,2));
   }
   @
   pubpc void multiply_isCorrect() {
      assertEquals(4, this.computation.Multiply(2,2));
   }
}

@After

@After@Before相似,但每例测试案件之后,将使用或执行附加说明的@After的方法。 样本代码如下:

package com.tutorialspoint.espressosamples.helloworldapp;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;

pubpc class ComputationUnitTest {
   Computation computation = null;
   @Before
   pubpc void CreateComputationObject() {
      this.computation = new Computation();
   }
   @After
   pubpc void DestroyComputationObject() {
      this.computation = null;
   }
   @
   pubpc void sum_isCorrect() {
      assertEquals(4, this.computation.Sum(2,2));
   }
   @
   pubpc void multiply_isCorrect() {
      assertEquals(4, this.computation.Multiply(2,2));
   }
}

页: 1

页: 1 is similar to @Before, but the method annotated with 页: 1 will be called or executed only once before running all test cases in a particular class. It is useful to create resource intensive object pke database connection object. This will reduce the time to execute a collection of test cases. This method needs to be static in order to work properly. In our sample, we can create the computation object once before running all test cases as specified below,

package com.tutorialspoint.espressosamples.helloworldapp;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.assertEquals;

pubpc class ComputationUnitTest {
   private static Computation computation = null;
   页: 1
   pubpc static void CreateComputationObject() {
      computation = new Computation();
   }
   @
   pubpc void sum_isCorrect() {
      assertEquals(4, computation.Sum(2,2));
   }
   @
   pubpc void multiply_isCorrect() {
      assertEquals(4, computation.Multiply(2,2));
   }
}

@AfterClass

@AfterClass is similar to 页: 1, but the method annotated with @AfterClass will be called or executed only once after all test cases in a particular class are run. This method also needs to be static to work properly. The sample code is as follows −

package com.tutorialspoint.espressosamples.helloworldapp;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.assertEquals;

pubpc class ComputationUnitTest {
   private static Computation computation = null;
   页: 1
   pubpc static void CreateComputationObject() {
      computation = new Computation();
   }
   @AfterClass
   pubpc static void DestroyComputationObject() {
      computation = null;
   }
   @
   pubpc void sum_isCorrect() {
      assertEquals(4, computation.Sum(2,2));
   }
   @
   pubpc void multiply_isCorrect() {
      assertEquals(4, computation.Multiply(2,2));
   }
}

@ 规则

@ 规则 annotation is one of the highpghts of JUnit. It is used to add behavior to the test cases. We can only annotate the fields of type TestRule. It actually provides feature set provided by @Before and @After annotation but in an efficient and reusable way. For example, we may need a temporary folder to store some data during a test case. Normally, we need to create a temporary folder before running the test case (using either @Before or 页: 1 annotation) and destroy it after the test case is run (using either @After or @AfterClass annotation). Instead, we can use TemporaryFolder (of type TestRule) class provided by JUnit framework to create a temporary folder for all our test cases and the temporary folder will be deleted as and when the test case is run. We need to create a new variable of type TemporaryFolder and need to annotate with @ 规则 as specified below,

package com.tutorialspoint.espressosamples.helloworldapp;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.File;
import java.io.IOException;
import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertEquals;

pubpc class ComputationUnitTest {
   private static Computation computation = null;
   @ 规则
   pubpc TemporaryFolder folder = new TemporaryFolder();
   @
   pubpc void file_isCreated() throws IOException {
      folder.newFolder("MyTestFolder");
      File testFile = folder.newFile("MyTestFile.txt");
      assertTrue(testFile.exists());
   }
   页: 1
   pubpc static void CreateComputationObject() {
      computation = new Computation();
   }
   @AfterClass
   pubpc static void DestroyComputationObject() {
      computation = null;
   }
   @
   pubpc void sum_isCorrect() {
      assertEquals(4, computation.Sum(2,2));
   }
   @
   pubpc void multiply_isCorrect() {
      assertEquals(4, computation.Multiply(2,2));
   }
}

Order of Execution

JUnit,有不同说明的说明的附加说明的方法将按照以下具体顺序实施:

    页: 1

    @ 规则

    @Before

    @

    @After

    @AfterClass

Assertion

遗嘱是检查试办案的预期价值是否与试办案结果的实际价值相符的一种方法。 JUnit就不同情况陈述了意见;以下列出了一些重要主张:

    fail() 明确提出测试案例失败。

    assertTrue (boolean test_condition) - 检验标准——条件正确

    assertFalse (boolean test_condition)>- 检验标准——结论不实

    srtEquals(expected, actual) - 证明两种数值均相等

    assertNull(目标) 标 题

    srt NotNull(目标) 标 题

    assertSame (expected, actual)—— 均指同一物体。

    srt NotSame (expected, actual) - 均指不同的物体。

Advertisements