提交 53ebbdb9 编写于 作者: W wizardforcel

2020-06-05 17:16:06

上级 982e063a
......@@ -210,7 +210,7 @@ void testCase()
> 阅读更多: [JUnit 5 断言](//howtodoinjava.com/junit-5/junit-5-assertions-examples/)
## 假设条件
## 假设
[`Assumptions`](http://junit.org/junit5/docs/current/api/org/junit/jupiter/api/Assumptions.html)类提供静态方法来支持基于假设的条件测试执行。 假设失败会导致测试中止。 通常在没有必要继续执行给定测试方法的情况下使用假设。 在测试报告中,这些测试将被标记为通过。
......
......@@ -118,7 +118,7 @@ void testCase() {
```
## 假设条件
## 假设
[假设](//howtodoinjava.com/junit-5/junit-5-assumptions-examples)提供了静态方法来支持基于假设的条件测试执行。 假设失败会导致测试中止。 通常在没有必要继续执行给定测试方法的情况下使用假设。 在测试报告中,这些测试将被标记为通过。
......
......@@ -2,27 +2,28 @@
> 原文: [https://howtodoinjava.com/junit5/junit-5-vs-junit-4/](https://howtodoinjava.com/junit5/junit-5-vs-junit-4/)
JUnit 5 旨在适应 Java 8 编码风格,并且比 JUnit 4 更加健壮和灵活。在本篇文章 **JUnit 5 vs JUnit 4** 中,我们将重点介绍 junit 4 和 junit 5 之间的一些主要区别。
JUnit 5 旨在适应 Java 8 编码风格,并且比 JUnit 4 更加健壮和灵活。在本篇文章 **JUnit 5 vs JUnit 4** 中,我们将重点介绍 junit 4 和 junit 5 之间的一些主要区别。
## 1\. JUnit 5 与 JUnit 4 –注释
## 1\. JUnit 5 与 JUnit 4 – 注释
两个版本中的大多数注释都相同,但几乎没有区别。 这是一个快速比较。
| 特征 | JUnit 4 | Junit 5 |
| 特性 | JUnit 4 | Junit 5 |
| --- | --- | --- |
| 声明测试方法 | `@Test` | `@Test` |
| 在当前类中的所有测试方法之前执行 | `@BeforeClass` | `@BeforeAll` |
| 在当前类中的所有测试方法之后执行 | `@AfterClass` | `@AfterAll` |
| 在每种测试方法之前执行 | `@Before` | `@BeforeEach` |
| 在每种测试方法之后执行 | `@After` | `@AfterEach` |
| 禁用测试方法/类 | `@Ignore` | `@Disabled` |
| 测试工厂进行动态测试 | 不适用 | `@TestFactory` |
| 进行动态测试的测试工厂 | 不适用 | `@TestFactory` |
| 嵌套测试 | NA | `@Nested` |
| 标记和过滤 | `@Category` | `@Tag` |
| 注册自定义扩展 | NA | `@ExtendWith` |
## 2\. JUnit 5 和 JUnit 4 之间的其他区别
#### 2.1 建筑
#### 2.1 架构
JUnit 4 将所有内容捆绑到单个 jar 文件中。
......@@ -36,7 +37,7 @@ Junit 5 由 3 个子项目组成,即 JUnit Platform,JUnit Jupiter 和 JUnit
它具有所有新的 junit 注解和`TestEngine`实现,以运行使用这些注解编写的测试。
3. ##### JUnit 复古
3. ##### JUnit Vintage
为了支持在 JUnit 5 平台上运行 JUnit 3 和 JUnit 4 书面测试。
......@@ -48,8 +49,8 @@ Junit 5 需要 Java 8 或更高版本。
#### 2.3 断言
在 Junit 4 中, [org.junit.Assert](http://junit.org/junit4/javadoc/4.12/org/junit/Assert.html) 具有所有肯定的方法来验证预期结果和结果。
他们接受错误消息的额外参数作为方法签名中的 FIRST 参数。 例如
在 Junit 4 中,[`org.junit.Assert`](http://junit.org/junit4/javadoc/4.12/org/junit/Assert.html)具有所有肯定的方法来验证预期结果和结果。
他们接受错误消息的额外参数作为方法签名中的第一个参数。 例如:
```java
public static void assertEquals(long expected, long actual)
......@@ -57,7 +58,8 @@ public static void assertEquals(String message, long expected, long actual)
```
在 JUnit 5 中, [org.junit.jupiter.Assertions](http://junit.org/junit5/docs/current/api/org/junit/jupiter/api/Assertions.html) 包含大多数断言方法,包括其他`assertThrows()``assertAll()`方法。 到目前为止,`assertAll()`处于实验状态,用于分组断言。
在 JUnit 5 中,[`org.junit.jupiter.Assertions`](http://junit.org/junit5/docs/current/api/org/junit/jupiter/api/Assertions.html)包含大多数断言方法,包括其他`assertThrows()``assertAll()`方法。 到目前为止,`assertAll()`处于实验状态,用于分组断言。
JUnit 5 断言方法还具有重载方法,以在测试失败(例如测试失败)时支持传递要打印的错误消息。
```java
......@@ -67,21 +69,21 @@ public static void assertEquals(long expected, long actual, Supplier messageSupp
```
#### 2.4 假设条件
#### 2.4 假设
在 Junit 4 中, [org.junit.Assume](http://junit.org/junit4/javadoc/4.12/org/junit/Assume.html) 包含一些方法,用于陈述有关测试有意义的条件的假设。 它具有以下五种方法:
在 Junit 4 中,[`org.junit.Assume`](http://junit.org/junit4/javadoc/4.12/org/junit/Assume.html)包含一些方法,用于陈述有关测试有意义的条件的假设。 它具有以下五种方法:
1. 假设为假()
2. 承担 NoException()
3. 假设不为空()
4. 假使,假设()
5. 假设 True()
1. `assumeFalse()`
2. `assumeNoException()`
3. `assumeNotNull()`
4. `assumeThat()`
5. `assumeTrue()`
在 Junit 5 中, [org.junit.jupiter.api.Assumptions](http://junit.org/junit5/docs/current/api/org/junit/jupiter/api/Assumptions.html) 包含一些方法,用于陈述有关测试有意义的条件的假设。 它具有以下三种方法:
在 Junit 5 中,[`org.junit.jupiter.api.Assumptions`](http://junit.org/junit5/docs/current/api/org/junit/jupiter/api/Assumptions.html) 包含一些方法,用于陈述有关测试有意义的条件的假设。 它具有以下三种方法:
1. 假设为假()
2. 假设那个()
3. 假设 True()
1. `assumeFalse()`
2. `assumingThat​()`
3. `assumeTrue()`
#### 2.5 标记和过滤
......@@ -91,7 +93,7 @@ public static void assertEquals(long expected, long actual, Supplier messageSupp
#### 2.6 测试套件
在 Junit 4 中,`@RunWith``@Suite`注解。 例如
在 Junit 4 中,`@RunWith``@Suite`注解。 例如
```java
import org.junit.runner.RunWith;
......@@ -108,7 +110,7 @@ public class JUnit4Example
```
在 Junit 5 中,`@RunWith``@SelectPackages``@SelectClasses`例如
在 Junit 5 中,`@RunWith``@SelectPackages``@SelectClasses`例如
```java
import org.junit.platform.runner.JUnitPlatform;
......
......@@ -6,7 +6,7 @@
## 安装
要将 JUnit 包含到您的项目中,您需要将其依赖项包含在 classpath 中。
要将 JUnit 包含到您的项目中,您需要将其依赖项包含在类路径中。
* #### JUnit Maven 依赖项
......@@ -32,11 +32,12 @@ dependencies {
单击链接以下载 [JUnit 4.12 jar 文件](http://central.maven.org/maven2/junit/junit/4.12/junit-4.12.jar)。
## JUnit 注
## JUnit 注
JUnit 提供以下注释来编写测试。
| 注解 | 描述 |
| --- | --- |
| `@Before` | 带注释的方法将在测试类中的每个测试方法之前运行。 |
| `@After` | 带注释的方法将在测试类中的每个测试方法之后运行。 |
| `@BeforeClass` | 带注释的方法将在测试类中的所有测试方法之前运行。 此方法必须是静态的。 |
......@@ -44,8 +45,8 @@ JUnit 提供以下注释来编写测试。
| `@Test` | 用于将方法标记为 junit 测试 |
| `@Ignore` | 它用于禁用或忽略测试套件中的测试类或方法。 |
| `@FixMethodOrder` | 此类允许用户选择测试类中方法的执行顺序。 |
| `@Rule` | 注释引用规则或返回规则的方法的字段。 |
| `@ClassRule` | 注释引用引用规则或方法的静态字段。 |
| `@Rule` | 注释引用规则的字段或返回规则的方法。 |
| `@ClassRule` | 注释引用规则的静态字段或返回它们的方法。 |
## 用 JUnit 编写测试
......@@ -107,7 +108,7 @@ public class JunitTestSuite {
## 断言
断言有助于通过测试用例的实际输出来验证期望的输出。 所有的断言都在 [org.junit.Assert](http://junit.org/junit4/javadoc/4.12/org/junit/Assert.html) 类中。 所有的 assert 方法都是`static`,它使代码可读性更好。
断言有助于通过测试用例的实际输出来验证期望的输出。 所有的断言都在[`org.junit.Assert`](http://junit.org/junit4/javadoc/4.12/org/junit/Assert.html)类中。 所有的断言方法都是`static`,它使代码可读性更好。
```java
import static org.junit.Assert.*;
......@@ -119,7 +120,7 @@ public void method() {
```
## 假设条件
## 假设
假设表明测试有意义的条件。 失败的假设并不意味着代码已损坏,但是测试没有提供有用的信息。 假设基本上是指“如果这些条件不适用,请勿运行此测试”。 默认的 JUnit 运行器将跳过带有失败假设的测试。
......
......@@ -6,8 +6,8 @@
在 JUnit 中,可以使用这些注释创建并执行测试套件。
1. **@RunWith**
2. **@SuiteClasses**
1. **`@RunWith`**
2. **`@SuiteClasses`**
> 阅读更多: [JUnit 5 测试套件](https://howtodoinjava.com/junit5/junit5-test-suites-examples/)
......
# JUnit JUnitCore 示例
# JUnit `JUnitCore`示例
> 原文: [https://howtodoinjava.com/junit/how-to-execute-junit-testcases-with-junitcore/](https://howtodoinjava.com/junit/how-to-execute-junit-testcases-with-junitcore/)
在任何以增量方式构建的应用中,通常希望只要引入新功能,我们就只能运行某些测试。 可以使用 JUnit 框架的`JUnitCore`类来实现。
[**JUnitCore**](http://junit.sourceforge.net/javadoc/org/junit/runner/JUnitCore.html "JUnitCore") 是 JUnit 包中的内置类,它基于**门面设计模式**`JUnitCore`类仅用于运行指定的测试类。
[**`JUnitCore`**](http://junit.sourceforge.net/javadoc/org/junit/runner/JUnitCore.html "JUnitCore")是 JUnit 包中的内置类,它基于**外观设计模式**`JUnitCore`类仅用于运行指定的测试类。
> 阅读更多: [JUnit 5 测试套件](https://howtodoinjava.com/junit5/junit5-test-suites-examples/)
## 1\. JUnitCore 示例
## 1\. `JUnitCore`示例
假设在应用发行版中有两个新功能。 这些功能通过两个接口公开。 假设接口名称为`FeatureOne``FeatureTwo`
......@@ -50,7 +50,7 @@ public class TestFeatureTwo {
```
#### 1.2 使用 JUnitCore 运行测试
#### 1.2 使用`JUnitCore`运行测试
要仅针对上述功能运行测试,我们可以编写这样的套件。
......@@ -97,7 +97,7 @@ public class WithJUnitCore
```
## 2\. JUnitCore 在命令提示符下运行测试
## 2\. `JUnitCore`在命令提示符下运行测试
要从命令提示符手动运行测试类,我们可以从控制台运行以下命令。 给出所有测试类的名称,以空格分隔。
......@@ -106,7 +106,7 @@ $ java org.junit.runner.JUnitCore TestFeatureOne TestFeatureTwo
```
## 3\. JUnitCore 运行所有测试
## 3\. `JUnitCore`运行所有测试
强烈建议创建 [JUnit 套件](https://howtodoinjava.com/junit/how-to-execute-junit-testcases-in-test-suite/),并在所有测试用例中执行应用。 这将需要一些工作,但是仍然是在 JUnit 中执行所有测试的最佳方法。
......@@ -123,4 +123,4 @@ public class TestFeatureSuite {
参考:
[JUnitCore Java 文档](https://junit.org/junit4/javadoc/4.12/org/junit/runner/JUnitCore.html)
\ No newline at end of file
[`JUnitCore` Java 文档](https://junit.org/junit4/javadoc/4.12/org/junit/runner/JUnitCore.html)
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册