JUnit初步理解

JUnit 是 Java 编程语言的单元测试框架。 JUnit 在测试驱动开发的开发中一直很重要,它是一组统称为 xUnit 的单元测试框架之一,它起源于 JUnit。

本文介绍了在使用 Java 时在项目单元测试中使用 JUnit。学习完成本系列文章后,您将获得有关使用 JUnit 测试框架的足够知识,您可以将自己提升到新的水平。

测试是检查应用程序功能以确保其按要求运行的过程。单元测试出现在开发人员级别;它是对单个实体(类或方法)的测试。单元测试在帮助软件公司向客户交付优质产品方面发挥着关键作用。

单元测试可以通过两种方式完成——手动测试和自动化测试。

Manual TestingAutomated Testing
Executing a test cases manually without any tool support is known as manual testing.Taking tool support and executing the test cases by using an automation tool is known as automation testing.
Time-consuming and tedious − Since test cases are executed by human resources, it is very slow and tedious.Fast − Automation runs test cases significantly faster than human resources.
Huge investment in human resources − As test cases need to be executed manually, more testers are required in manual testing.Less investment in human resources − Test cases are executed using automation tools, so less number of testers are required in automation testing.
Less reliable − Manual testing is less reliable, as it has to account for human errors.More reliable − Automation tests are precise and reliable.
Non-programmable − No programming can be done to write sophisticated tests to fetch hidden information.Programmable − Testers can program sophisticated tests to bring out hidden information.

JUnit是什么?

JUnit 是 Java 编程语言的单元测试框架。它在测试驱动开发中发挥着至关重要的作用,是一系列单元测试框架,统称为 xUnit。

JUnit提倡“先测试后编码”的思想,强调为一段代码设置测试数据,可以先测试后实现。这种方法就像“测试一点,编码一点,测试一点,编码一点”。它提高了程序员的工作效率和程序代码的稳定性,从而减少了程序员的压力和调试时间。

JUnit的特点

1、JUnit 是一个开源框架,用于编写和运行测试。

2、提供注释以识别测试方法。

3、为测试预期结果提供断言。

4、为运行测试提供测试运行器。

5、JUnit 测试允许您更快地编写代码,从而提高质量。

6、JUnit 非常简单。它不那么复杂,花费的时间也更少。

7、JUnit 测试可以自动运行,它们会检查自己的结果并提供即时反馈。无需手动梳理测试结果报告。

8、JUnit 测试可以组织成包含测试用例甚至其他测试套件的测试套件。

9如果测试运行顺利,JUnit 会在一个绿色条中显示测试进度,而当测试失败时,它会变为红色。

什么是单元测试用例?

单元测试用例是代码的一部分,它确保另一部分代码(方法)按预期工作。为了快速达到预期的结果,需要一个测试框架。 JUnit 是一个完美的 Java 编程语言单元测试框架。

正式的书面单元测试用例的特点是已知输入和预期输出,这是在执行测试之前制定的。已知输入应测试前置条件,预期输出应测试后置条件。

每个需求必须至少有两个单元测试用例——一个正面测试和一个负面测试。如果一个需求有子需求,那么每个子需求必须至少有两个测试用例,分别是肯定的和否定的。

 

单元测试基本原则

1、每个单元测试都必须独立于其它所有单元测试而运行。

2、框架应该以单个测试为单元来检测和报告错误。

3、应该易于定义要运行哪些单元测试。

版权声明:著作权归作者所有。

thumb_up 1 | star_outline 0 | textsms 0