大数跨境
0
0

Software Testing Lesson 7 Unit Testing Tutorial

Software Testing Lesson 7  Unit Testing Tutorial 豆豆咨询
2020-02-29
1
导读:Unit Testing Tutorial: What is, Types, Tools, EXAMPLEWhat is Unit Testing?

What is Unit Testing?


UNITTESTING is a type of software testingwhere individual units or components of a software are tested. The purpose isto validate that each unit of the software code performs as expected. UnitTesting is done during the development (coding phase) of an application by thedevelopers. Unit Tests isolate a section of code and verify its correctness. Aunit may be an individual function, method, procedure,

In SDLC, STLC, V Model, Unit testing is firstlevel of testing done before integration testing. Unit testing is a WhiteBoxtesting technique that is usually performed by the developer. Though, in apractical world due to time crunch or reluctance of developers to tests, QAengineers also do unit testing.

In this tutorial, youwill learn-

  • Why Unit Testing?

  • How to do Unit Testing

  • Unit Testing Techniques

  • Unit Testing Tools

  • Test Driven Development (TDD) & Unit Testing

  • Unit Testing Myth

  • Unit Testing Advantage

  • Unit Testing Disadvantages

  • Unit Testing Best Practices

Why Unit Testing?

Sometimes software developersattempt to save time by doing minimal unit testing. This is a myth becauseskipping on unit testing leads to higher Defect fixing costs during System TestingIntegrationTesting and even Beta Testing afterthe application is completed. Proper unit testing done during the developmentstage saves both time and money in the end. Here, are key reasons to performunit testing.

Unit Testing Levels

1.      Unit tests help to fix bugs early in the development cycle and save costs.

2.      It helps the developers to understand the code base and enables them tomake changes quickly

3.      Good unit tests serve as project documentation

4.      Unit tests help with code re-use. Migrate both your code andyourtests to your new project. Tweak the code until the tests run again.

How to do Unit Testing

Unit Testing is of twotypes

  • Manual

  • Automated

Unit testing iscommonly automated but may still be performed manually. Software Engineeringdoes not favor one over the other but automation is preferred. A manualapproach to unit testing may employ a step-by-step instructional document.

Under the automated approach-

  • A developer writes a     section of code in the application just to test the function. They would     later comment out and finally remove the test code when the application is     deployed.

  • A developer could also     isolate the function to test it more rigorously. This is a more thorough     unit testing practice that involves copy and paste of code to its own     testing environment than its natural environment. Isolating the code helps in revealing     unnecessary dependencies between the code being tested and other units or     data spaces in the product. These dependencies can     then be eliminated.

  • A coder generally uses a     UnitTest Framework to develop automated test cases. Using an automation     framework, the developer codes criteria into the test to verify the     correctness of the code. During execution of the test cases, the framework     logs failing test cases. Many frameworks will also automatically flag and     report, in summary, these failed test cases. Depending on the severity of     a failure, the framework may halt subsequent testing.

  • The workflow of Unit     Testing is 1) Create Test Cases 2) Review/Rework 3) Baseline 4) Execute     Test Cases.

Unit Testing Techniques

Code coveragetechniques used in united testing are listed below:

  • Statement Coverage

  • Decision Coverage

  • Branch Coverage

  • Condition Coverage

  • Finite State Machine     Coverage

For more in refer https://www.guru99.com/code-coverage.html

Unit Testing Example: Mock Objects

Unit testing relies onmock objects being created to test sections of code that are not yet part of acomplete application. Mock objects fill in for the missing parts of theprogram.

For example, you mighthave a function that needs variables or objects that are not created yet. Inunit testing, those will be accounted for in the form of mock objects createdsolely for the purpose of the unit testing done on that section of code.

Unit Testing Tools

There are severalautomated tools available to assist with unit testing. We will provide a fewexamples below:

1.     Junit: Junit is a free to use testing tool used for Java programminglanguage.  It provides assertions to identify test method. This tool testdata first and then inserted in the piece of code.

2.     NUnit:  NUnit is widely used unit-testing framework use for all.net languages.  It is an open source tool which allows writing scriptsmanually. It supports data-driven tests which can run in parallel.

3.     JMockit:  JMockit is open source Unit testing tool.  It is acode coverage tool with line and path metrics. It allows mocking API withrecording and verification syntax. This tool offers Line coverage, PathCoverage, and Data Coverage.

4.     EMMA:  EMMA is an open-source toolkit for analyzing andreporting code written in Java language. Emma support coverage types likemethod, line, basic block. It is Java-based so it is without external librarydependencies and can access the source code.

5.     PHPUnit: PHPUnit is a unit testing tool for PHP programmer. It takessmall portions of code which is called units and test each of them separately. The tool also allows developers to use pre-define assertion methods toassert that a system behave in a certain manner. 

Those are just a few ofthe available unit testing tools. There are lots more, especially for Clanguages and Java, but you are sure to find a unit testing tool for yourprogramming needs regardless of the language you use.

Test Driven Development (TDD) & Unit Testing

Unit testing in TDDinvolves an extensive use of testing frameworks. A unit test framework is usedin order to create automated unit tests. Unit testing frameworks are not uniqueto TDD, but they are essential to it. Below we look at some of what TDD bringsto the world of unit testing:

  • Tests are written before     the code

  • Rely heavily on testing     frameworks

  • All classes in the     applications are tested

  • Quick and easy     integration is made possible

Unit Testing Myth

Myth: It requires time,and I am always overscheduled
My code is rock solid! I do not need unit tests.

Myths by their verynature are false assumptions. These assumptions lead to a vicious cycle asfollows –

Truth is Unit testingincrease the speed of development.

Programmers think that IntegrationTesting will catch all errors and do not execute the unit test. Once units areintegrated, very simple errors which could have very easily found and fixed inunit tested take a very long time to be traced and fixed.

Unit Testing Advantage

  • Developers looking to     learn what functionality is provided by a unit and how to use it can look     at the unit tests to gain a basic understanding of the unit API.

  • Unit testing allows the     programmer to refactor code at a later date, and make sure the module     still works correctly (i.e. Regression testing). The procedure is to write     test cases for all functions and methods so that whenever a change causes     a fault, it can be quickly identified and fixed.

  • Due to the modular nature     of the unit testing, we can test parts of the project without waiting for     others to be completed.

Unit Testing Disadvantages

  • Unit testing can't be     expected to catch every error in a program. It is not possible to evaluate     all execution paths even in the most trivial programs

  • Unit testing by its very     nature focuses on a unit of code. Hence it can't catch integration errors     or broad system level errors.

It's recommended unittesting be used in conjunction with other testing activities.

Unit Testing Best Practices

  • Unit Test cases should be     independent. In case of any enhancements or change in requirements, unit     test cases should not be affected.

  • Test only one code at a     time.

  • Follow clear and     consistent naming conventions for your unit tests

  • In case of a change in     code in any module, ensure there is a corresponding unit Test     Case for the module, and the module     passes the tests before changing the implementation

  • Bugs identified during     unit testing must be fixed before proceeding to the next phase in SDLC

  • Adopt a "test as     your code" approach. The more code you write without testing, the     more paths you have to check for errors.

Summary

  • UNIT TESTING is defined     as a type of software testing where individual units or components of a     software are tested.

  • As you can see, there can     be a lot involved in unit testing. It can be complex or rather simple     depending on the application being tested and the testing strategies,     tools and philosophies used. Unit testing is always necessary on some     level. That is a certainty.


【声明】内容源于网络
0
0
豆豆咨询
提供前沿的信息技术咨询,实用的编程、项目管理方法,优质的专家服务,如PHP、Java、C#、C++、ASP.NET、ThinkPHP、Git、Matlab、图像处理、数据库、云计算、科技论文撰写等。
内容 44
粉丝 0
豆豆咨询 提供前沿的信息技术咨询,实用的编程、项目管理方法,优质的专家服务,如PHP、Java、C#、C++、ASP.NET、ThinkPHP、Git、Matlab、图像处理、数据库、云计算、科技论文撰写等。
总阅读15
粉丝0
内容44