Generating and Testing QR Codes for Inventory Management in QA
Tips and Tricks to using QR Codes
In quality assurance (QA), ensuring the accuracy and functionality of inventory management systems is critical. One effective way to test product IDs in an inventory application is by generating and validating QR codes. In this blog post, I’ll walk you through how I used Python to create custom QR codes for testing product IDs and share insights on integrating this into your QA testing workflow.
Why QR Codes for QA Testing?
QR codes are a robust way to encode product IDs, allowing for quick scanning and validation in inventory applications. By generating QR codes programmatically, QA teams can:
- Simulate real-world product data.
- Test scanning functionality and data accuracy.
- Automate validation of product IDs against a database.
- Ensure error handling for malformed or invalid IDs.
In this example, I focused on testing a set of product IDs for an inventory application, generating QR codes for each ID and saving them as images for further testing.
Sample Sheet that I created. I used PhotoScape to combine the images and added the text below the QA Code.
The Python Code
Below is the Python script I used to generate QR codes for a list of product IDs. The script uses the qrcode
library to create QR codes and save them as PNG files.
#!/usr/bin/python3
import datetime
import qrcode
# List of QR Code data (product IDs)
qr_codes = [
"22001185000-TV825862",
"46002897000-AV301140",
"46002897000-AV320052",
"46003686000-TS062286",
"46200239000-HRB3019056",
"46039182000-HRC0994569",
"22031593000-0hfg7ddw502291r",
"46003251000-BA30816015",
"46002566000-VT5575px;32991",
"22010473000-zt600331",
"46200240000-MLSVTXLHQ-7",
"46200240000-HRC2167455",
"46074803000-MM0O05JGX-0",
"46074803000-HRB2539864",
"46088955000-kc1008395"
]
# Function to generate and save QR codes
def generate_qr_codes(codes):
for i, code in enumerate(codes):
# Create QR code instance
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data(code)
qr.make(fit=True)
# Create an image from the QR Code instance
img = qr.make_image(fill='black', back_color='white')
# Save the image to a file
img.save(f"{code}.png")
# Generate and save QR codes
generate_qr_codes(qr_codes)
How It Works
- Dependencies: The script uses the
qrcode
library, which you can install viapip install qrcode pillow
. ThePillow
library is required for image handling. - Product IDs: The
qr_codes
list contains product IDs in various formats, simulating real-world data with different prefixes, suffixes, and lengths. - QR Code Generation:
- A
QRCode
instance is created with:version=1
(smallest QR code size, sufficient for short IDs).error_correction=ERROR_CORRECT_L
(low error correction, suitable for most use cases).box_size=10
(pixel size of each QR code module).border=4
(white space around the QR code).
- Each product ID is added to the QR code, and the
make
method generates the QR code matrix.
- A
- Image Creation and Saving: The QR code is converted to a black-and-white PNG image and saved with the product ID as the filename (e.g.,
22001185000-TV825862.png
).
QA Testing Workflow
Here’s how I incorporated this script into my QA testing process:
- Generate QR Codes:
- Run the script to create QR code images for all product IDs.
- Verify that each image is created and named correctly.
- Test Scanning Functionality:
- Use a QR code scanner (e.g., a mobile app or the inventory application’s scanner) to read each QR code.
- Validate that the scanned data matches the original product ID.
- Validate Against Database:
- Check that the scanned product ID exists in the inventory database.
- Test edge cases, such as invalid or duplicate IDs.
- Error Handling:
- Test malformed product IDs (e.g., too long, special characters).
- Ensure the application handles scanning errors gracefully.
Lessons Learned
- Data Diversity: Include a variety of product ID formats in your test data to mimic real-world scenarios.
- Scalability: For large datasets, consider batch processing or parallelizing QR code generation to save time.
- Validation: Always validate QR code content programmatically to catch discrepancies early.
- Error Correction: Adjust the
error_correction
level (e.g.,ERROR_CORRECT_M
orERROR_CORRECT_H
) if QR codes will be printed or scanned in challenging conditions.