iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
😺

Learn by Implementing a Pairwise Testing Algorithm

に公開

What is Pairwise Testing?

In this article, to learn about the pairwise testing algorithm, I referred to Pairwise Testing with QICT and implemented (or rather, mostly transcribed) something called node-qict in Node.js.
I have also uploaded it to npm, so you can execute it from the command line or integrate it into various tools.
Pairwise testing is a combinatorial testing technique used to reduce the number of test sets when there are too many combinations to test.
Quoting from Pairwise Testing:
"It is based on the observation that most defects are caused by interactions of at most two factors."
A test set generated by the pairwise method covers all combinations of two elements.
Also, I will explain the implementation details of QICT later, but it combines probabilistic factors.
Now, let's look at the source code of node-qict step-by-step to see how the above is achieved.

Input File Format

Let's consider a certain web application.

Parameter (Factor) Parameter Values (Levels)
Switch on,off
Browser Chrome, Firefox, Opera, Lynx
OS Windows, Mac, Linux
Membership Member, Guest
If you want to output the pairwise combinations for the Parameters and Parameter Values above, the input file format would be as follows:
$ cat   __tests__/testData.txt
Switch: on, off
Browser: Chrome, Firefox, Opera, Lynx
OS: Windows, Mac, Linux
Membership: Member, Guest

In other words, prepare a text file where the delimiter between Parameters and Parameter Values is ":", and the delimiter within Parameter Values is ",".

Installation Method

To install via the command line, assuming Node.js is installed, you can use the following method:

$ npm i -g qict

Also, if you want to try it out in your browser immediately, please check GitHub Pages.

Implementation Details

The overall flow is as follows:

  • readFile(file): Read the file
  • initialize(): Initialize all parameters
  • testSets(): Generate test sets
    • while(this.unusedPairs.length > 0): Until there are no more unused pairs
      • candidateSets = _candidateSets(): Generate candidate test sets
          • best = _best()
          • ordering = _ordering(best)
          • testSet = _testSet(best,ordering)
      • bestCandidate = _bestCandidate(candidateSets): Select the highest priority test set
      • _modifyUnused(bestCandidate)
  • printResult(testSets): Display the results

readFile(file) Reading from a file

This is simple. From the "file" argument:

  • Read everything using readFileSync
  • Convert it to a string
  • trim() it
  • Store it in this.contents

That's it.

  /**
   * readFile - store content from file
   * @param {string} file - target File
   */
  readFile(file){
    const fs = require('fs');
    this.contents = fs.readFileSync(file, 'utf8').trim();
  }

initialize() Initializing all parameters

This method is divided into a first half and a second half.

First half: Filling parameters, parameterValues, legalValues, and parameterPositions

  • Step 1: Process this.contents line by line, which was read by readFile(file).
  • Step 2: Split each line by ":" to create pairs.
  • Step 3: Push pair[0] into this.parameters.
  • Step 4: Split pair[1] by "," to create an array.
  • Step 5: Push all values split in Step 4 into this.parameterValues.
  • Step 6: Create legalValues (incidentally, legalValues are replaced with numbers for easier handling).
  • Step 7: Calculate which Parameter each of the 11 parameterValues belongs to for parameterPositions.
Switch: on, off
Browser: Chrome, Firefox, Opera, Lynx
OS: Windows, Mac, Linux
Membership: Member, Guest

After analyzing the above, each parameter should look like this:

    this.parameters = ["Switch","Browser","OS","Membership"];
    this.parameterValues = ["on","off","Chrome","Firefox","Opera","Lynx","Windows","Mac","Linux","Member","Guest"];
    this.legalValues = [
     [0,1],
     [2,3,4,5],
     [6,7,8],
     [9,10]
    ];
    this.parameterPositions = [
     0,0,
     1,1,1,1,
     2,2,2,
     3,3
    ];

Second half: Calculating combinations

All possible combinations of Parameter Values should result in a matrix like the one below.

unusedPairs on off Chrome Firefox Opera Lynx Windows Mac Linux Member Guest
on 0 0 1 1 1 1 1 1 1 1 1
off 0 0 1 1 1 1 1 1 1 1 1
Chrome 0 0 0 0 0 0 1 1 1 1 1
Firefox 0 0 0 0 0 0 1 1 1 1 1
Opera 0 0 0 0 0 0 1 1 1 1 1
Lynx 0 0 0 0 0 0 1 1 1 1 1
Windows 0 0 0 0 0 0 0 0 0 1 1
Mac 0 0 0 0 0 0 0 0 0 1 1
Linux 0 0 0 0 0 0 0 0 0 1 1
Member 0 0 0 0 0 0 0 0 0 0 0
Guest 0 0 0 0 0 0 0 0 0 0 0

This is represented by a multi-dimensional array called unusedPairs.
Let's calculate unusedCounts by counting each item from this matrix.

unusedCounts
on 9
off 9
Chrome 7
Firefox 7
Opera 7
Lynx 7
Windows 8
Mac 8
Linux 8
Member 9
Guest 9

_candidateSets() Create as many candidate test sets as this.pool

This function extracts candidate test sets in the following three steps:

["on", "Chrome", "Windows", "Member" ]

Like the example above.

Step 1: _best() Select the highest priority pair from all parameterValues

Calculates the optimal pair of parameterValues.
The algorithm sums the unused counts of two parameter values and selects the one with the highest total.
Specifically, it looks like this:

let weight = this.unusedCounts[pair[0]] + this.unusedCounts[pair[1]];

Step 2: _ordering() Create a priority order for Parameters

To make it easier to understand, let's assume that ParameterValues 0 and 9, namely "on" and "Member", have been selected.
Let's review this.ParameterPositions once more:

this.parameterPositions = [
"0",0,
1,1,1,1,
2,2,2,
"3",3
];

From this, the Parameters for "on" and "Member" are "0" and "3" respectively.
In other words, prioritizing those, the priority order for Parameters will be [0, 3, ?, ?].
The latter ? and ? will be selected randomly here.
So it would be something like:
[0, 3, 1, 2]
or
[0, 3, 2, 1]
.

Step 3: _testSet() Create one test set

The highest priority ParameterValue has already been decided by _best(), and the priority ordering is also fixed.
In other words, the current state of the testSet is as follows:

Selected Value
Switch on
Browser ?
OS ?
Membership Member

So how should we select the ? parts for Browser and OS?
The algorithm is as follows:

The following logic is executed for all undetermined ParameterValues, in this case, "Browser" and "OS":

  • Step 1: First, create pairs of [candidate ParameterValue, already determined ParameterValue].
    • In this case, the first pair would sequentially be ["Chrome", "on"].
  • Step 2: Check the unusedCount of this pair. Since ordering is in a random sequence, both ["Chrome", "on"] and ["on", "Chrome"] are checked.
  • Step 3: As a result of Step 2, the Parameter Value with the highest number of unused counts is selected.

The locations for ["Chrome", "on"] and ["on", "Chrome"] are shown in bold in the table below.

unusedPairs on off Chrome Firefox Opera Lynx Windows Mac Linux Member Guest
on 0 0 1 1 1 1 1 1 1 1 1
off 0 0 1 1 1 1 1 1 1 1 1
Chrome 0 0 0 0 0 0 1 1 1 1 1
Firefox 0 0 0 0 0 0 1 1 1 1 1
Opera 0 0 0 0 0 0 1 1 1 1 1
Lynx 0 0 0 0 0 0 1 1 1 1 1
Windows 0 0 0 0 0 0 0 0 0 1 1
Mac 0 0 0 0 0 0 0 0 0 1 1
Linux 0 0 0 0 0 0 0 0 0 1 1
Member 0 0 0 0 0 0 0 0 0 0 0
Guest 0 0 0 0 0 0 0 0 0 0 0

_bestCandidate() Selecting the Optimal Test Set

Count all unused combinations within the test set.
As an example, let's say

["on", "Chrome", "Windows", "Member" ]

was selected.
All the combination pairs for this would be:

["on", "Chrome"]
["on", "Windows"]
["on", "Member"]
["Chrome", "Windows"]
["Chrome", "Member"]
["Windows", "Member"]

Count the unused count for all six of these,
and select the one with the highest count as the bestCandidate.

_modifyUnused() Cleaning up unusedCount, unusedPairs, and unusedPairsSearch

For all pairs in the selected test set:

["on", "Chrome"]
["on", "Windows"]
["on", "Member"]
["Chrome", "Windows"]
["Chrome", "Member"]
["Windows", "Member"]
  • Step 1: Decrement the unusedCount.
  • Step 2: Remove from unusedPairs.
  • Step 3: Set the corresponding location in unusedPairsSearch to "0".

Repeat these steps until there are no more unusedPairs.

  • readFile(file): Read the file
  • initialize(): Initialize all parameters
  • testSets(): Generate test sets
    • while(this.unusedPairs.length > 0): Until there are no more unused pairs
      • candidateSets = _candidateSets(): Generate candidate test sets
          • best = _best()
          • ordering = _ordering(best)
          • testSet = _testSet(best,ordering)
      • bestCandidate = _bestCandidate(candidateSets): Select the highest priority test set
      • _modifyUnused(bestCandidate)
  • printResult(testSets): Display the results

Finally, display the results and finish.

Finally

Well, it was a bit long, but did you understand how it is implemented?
By the way, like any other method, this pairwise approach is not a silver bullet.
Please use it in the right places, just like other methods such as equivalence partitioning, boundary value analysis, or random input.
Also, pull requests for node-qict are very welcome.
Thank you very much.

Discussion