Partitioning - aapowers/BroadleafCommerce GitHub Wiki
Functional Testing: Partitioning
Systematic Functional Testing and Partition Testing
Functional testing refers to creating and running tests based on the specified functionality for a program. Systematic functional testing means that as we do this, we specifically try to choose valuable inputs for those tests. This might include choosing cases that are likely to fail.
Systematic partition testing is a way of helping us choose those valuable inputs. This is done by identifying groups, or partitions, of possible inputs that should all result in a similar output. By choosing an input from each partition, our tests are more likely to find errors.
Populate ISO Feature
The feature that I will focus on partition testing is the population of the ISO code for a given address.
ISO codes, published by the International Organization for Standardization, identify countries and subdivisions. According to the ISO website,
ISO 3166 has three parts: codes for countries, codes for subdivisions and formerly used codes (codes that were once used to describe countries but are no longer in use).
The country codes can be represented either as a two-letter code (alpha-2) which is recommended as the general-purpose code, a three-letter code (alpha-3) which is more closely related to the country name and a three-digit numeric code (numeric-3) which can be useful if you need to avoid using Latin script.
The codes for subdivisions are represented as the alpha-2 code for the country, followed by up to three characters. For example ID-RI is the Riau province of Indonesia and NG-RI is the Rivers province in Nigeria.
However, the ISO code population feature for BroadleafCommerce simplifies the country code to always use the alpha-2 (two-letter) code.
The feature could be written as:
As developer implementing an e-commerce site, I want to get the ISO code for an address, so that I can easily determine if the address is in a region that I ship to.
Conditions of Satisfaction:
- Given an address with a valid country and a valid state, province, or region, the address should have an ISO code.
- Given an address with an invalid country or state/province/region, the address should not have an ISO code.
Partitions
There are two large partitions for this feature: valid input and invalid input. Valid input should result in the ISO code getting set for the address and invalid input should result in no ISO code getting set for the address. Both partitions have sub-partitions that can be found in the tables below.
Valid Input
Partition | Example | Expected ISO |
---|---|---|
Country with a State | California, US | US-CA |
Country with a Province | Quebec, CA | CA-QC |
Country with a Region | Normandie, France | FR-NOR |
Subdivision with 2 letters | California, US | US-CA |
Subdivision with 3 letters | Normandie, France | FR-NOR |
Subdivision with numbers | Seoul, South Korea | KR-11 |
Invalid Input
Partition | Example | Expectation |
---|---|---|
No country | -- | no ISO code |
No state/region/province | -- | no ISO code |
Country does not exist | California, Codeland | no ISO code |
State/region/province does not exist | 999, US | no ISO code |
Country + state is not a valid combination | California, France | no ISO code |
Note: Some of the partitions have overlapping input and can be condesed into one test. For example,
US-CA
falls into the Country with a State
partition and the Subdivision with 2 letters
partition.
Therefore, both partitions are covered by the same test.
Unit Tests
In AddressServiceImplTest.java
, I wrote two unit tests to cover the two broad partitions.
Both tests handle every partition from their corresponding table above.
Valid Input Test
The valid input test fetches a data set of valid addresses from the partitions listed in Valid Input table above. It then loops over each address, calls the function to populate the ISO code, and asserts that the address has the expected ISO code.
Invalid Input Test
The invalid input test fetches a data set of invalid addresses from the partitions listed in the Invalid Input table above. It then loops over each address, calls the function to populate the ISO code, and asserts that the address does not have an associated ISO code.
Running the tests
The test code is available on GitHub.
To run the tests, open the file AddressServiceImplTest.java
, right click on the class name,
and click Run AddressServiceImplTest
: