Negative test data

A negative test needs two things: a value your application should refuse, and a way to check that it did. One run of qadg gives you both, and valid values that careless validation refuses as well.

The answer matters as much as the value

A file of bad values is easy to make. The hard part is the test that reads the result: for every row it has to know whether your application should have refused it. Written by hand, that list of answers falls behind the data the first time the data changes.

QA Data Generator writes the answers itself. Every value it damages gets an entry in expectation-manifest.json: where it is, the value before and after, and what your application should do with it. Your test reads the answer from the manifest instead of keeping its own.

Three kinds of answer

  • Reject. The value breaks a rule every application of its kind should keep: a name longer than its column, a card number whose check digit no longer matches.
  • Accept. The value is valid, and careless validation refuses it: O’Brien, Łukasz, 佐藤, the surname Null. A negative test that never sends these misses the people your application turns away. Names your form should accept has more.
  • ~, your call. The right answer depends on your own rules. An empty name or an SSN from an area that is never issued should usually be refused, but only you know whether your application checks for it. qadg verify reports a disagreement on one of these and does not fail on it.

A recipe that asks for all three

The recipe below describes a table of customers: names that are required and have a longest length, an SSN and a card number. Its policy asks for damage from three groups, Field format (L2), Value meaning (L3) and Valid but unusual values (REV), in about two rows in five, one value in each, so every refused row has a single cause.

negative-test-data.yaml
# Customers with one value changed in about two fifths of the rows: values your
# application should refuse, and real names it should take.
#
# The names are required and have a longest length, and the card number carries
# a check digit. The damage makes a name too long or empty, moves the check
# digit of the card or swaps two of its digits, gives the SSN the area 000,
# which is never issued, or puts in a name such as O'Brien or Null. The manifest
# gives the answer for every value it changed, and lists the damage this build
# could not carry out, with the reason.
#
#   qadg generate negative-test-data.yaml
#
# writes customer.csv, expectation-manifest.json and run-note.json. The same
# seed changes the same values on every machine.
version: "0.1"
seed: "negative-test-data"
locale: en-US
entities:
  customer:
    count: 100
    primary_key: [ssn]
    fields:
      first_name: { generator: first_name, required: true, max_length: 30 }
      last_name:  { generator: last_name, required: true, max_length: 30 }
      ssn:        { generator: ssn, unique: true }
      card:       { generator: card }
policy:
  layers: [L2, L3, REV]
  rows: 40%
  per_row: exactly_one
output:
  format: csv

What it changed

Save the recipe, then run this in the same folder. This is what the program printed for it.

terminal
$ qadg generate negative-test-data.yaml
       customer: 100 rows, 4 columns
1 table, 100 rows written
42 defects ordered, 16 applied, manifest in expectation-manifest.json
26 defects could not be applied, and the manifest carries the reason for each one
  1 because the dataset has no column of the kind they apply to
  25 because this build has no implementation of them yet

The recipe ordered 42 defects, and this build applied the 16 below. The other 26 are in the manifest as not applied, each with its reason, and the run above counts them by reason.

L3.checksum.off_by_onereject

customer 7, column card

6243030000000001 → 6243030000000002

L3.checksum.transposed_digitsreject

customer 10, column card

4242424242424242 → 4244224242424242

REV.name.apostropheaccept

customer 26, column last_name

Ayers → D'Arcy

REV.name.cjkaccept

customer 34, column last_name

Morton → 诸葛

L2.string.empty~ reject

customer 45, column first_name

Maureen → empty

L3.range.unassigned~ reject

customer 46, column ssn

666-82-9772 → 000-82-9772

L2.string.exceeds_maxlengthreject

customer 47, column first_name

Alexandra → Alexandraaaaaaaaaaaaaaaaaaaaaaa

REV.name.diacriticsaccept

customer 48, column last_name

Ellis → Dvořák

REV.name.null_surnameaccept

customer 55, column last_name

Zuniga → Null

L3.checksum.off_by_onereject

customer 57, column card

6243030000000001 → 6243030000000002

L3.checksum.transposed_digitsreject

customer 70, column card

3530111333300000 → 3350111333300000

REV.name.apostropheaccept

customer 86, column last_name

Sharp → O'Brien

REV.name.cjkaccept

customer 93, column last_name

Stevens → 欧阳

L2.string.empty~ reject

customer 94, column first_name

Kimberly → empty

L3.range.unassigned~ reject

customer 99, column ssn

666-83-6905 → 000-83-6905

L2.string.exceeds_maxlengthreject

customer 100, column first_name

Dominic → Dominiccccccccccccccccccccccccc

Some of the damage it ordered is not carried out by this version yet. The manifest lists each one with the reason, so a run that changed less than the recipe asks for says so.

Put it in your test

  1. Generate the data from the recipe.
  2. Send each row to your application the way a user would: through the form, the API or the import.
  3. Write down in a small JSON file what your application did with each row, accepted or rejected.
  4. Run qadg verify on the manifest and that file. It lists every row your application treated differently, and ends with exit code 4, so a pipeline can fail on it.

Check what your app did shows the record and the report. Change layers to choose what to attack, rows for how much, and seed for other values of the same kinds. What it breaks lists every kind of damage this version carries out.