This page documents the comprehensive testing infrastructure and example documents provided with the gobl.ubl library. The test suite validates bidirectional conversion between GOBL and UBL formats across multiple contexts (EN16931, Peppol, XRechnung, French CIUS) and provides example documents for common invoice scenarios.
For information about the core conversion functions being tested, see Core API Functions. For details on the context system used in testing, see Context and Profile System.
The testing framework serves three primary purposes:
The test infrastructure is located in examples_test.go1-433 and uses the testify assertion library along with optional Phive validation for XML compliance checking.
The testing system is organized around two main test functions that validate conversion in both directions, supported by helper functions for loading and processing test documents.
Test Framework Overview
Sources: examples_test.go42-122 examples_test.go124-220
The test data is organized in a hierarchical directory structure under test/data/ with separate folders for conversion direction and context type:
| Directory | Purpose | Format | Description |
|---|---|---|---|
test/data/convert/ | GOBL to UBL | Input: JSON | Source GOBL envelopes for conversion testing |
test/data/convert/{context}/out/ | GOBL to UBL | Output: XML | Expected UBL output files (golden files) |
test/data/parse/ | UBL to GOBL | Input: XML | Source UBL documents for parsing testing |
test/data/parse/{context}/out/ | UBL to GOBL | Output: JSON | Expected GOBL output files (golden files) |
Each context directory (en16931, peppol, peppol-self-billed, xrechnung, france-cius, france-extended) contains example documents specific to that UBL profile.
Sources: examples_test.go383-404
The TestConvertToInvoice function validates the GOBL to UBL conversion path across all supported contexts.
GOBL to UBL Test Flow
Sources: examples_test.go42-122
The test function defines six context configurations:
Each context has its own test subdirectory and is tested independently using nested t.Run() calls for clear test output organization.
Sources: examples_test.go56-68
To ensure consistent test output, the test framework replaces document UUIDs with a static value:
This normalization occurs in the loadTestEnvelopeFromPath function and affects both the envelope's head UUID and the invoice's document UUID.
Sources: examples_test.go33 examples_test.go258-261
The TestParseInvoice function validates the UBL to GOBL conversion path, parsing XML documents and comparing the resulting GOBL invoices against golden files.
The parsing test follows this sequence:
test/data/parse/{context}/*.xmlubl.Parse() to deserialize XML into intermediate UBL structuresInvoice.Convert() to transform UBL to GOBL envelopeSources: examples_test.go124-220
The parsing tests include an important comment about validation:
This indicates that the test suite uses real-world UBL examples that may not fully comply with GOBL's stricter validation rules, reflecting the reality that UBL documents in the wild often contain minor compliance issues.
Sources: examples_test.go164-168
The test extracts only the invoice portion from the envelope for comparison, ignoring envelope-level metadata differences:
Invoice-Level Comparison Strategy
Sources: examples_test.go192-215
The test suite includes several example document types demonstrating various invoice scenarios:
| Example File | Type | Notable Features |
|---|---|---|
invoice-minimal.json | Standard Invoice | Minimal required fields, credit transfer payment |
invoice-complete.json | Standard Invoice | All optional fields populated, endpoint IDs |
invoice-with-line-order.json | Standard Invoice | Line-level order references, delivery details |
invoice-without-buyers-tax-id.json | Standard Invoice | Missing customer tax ID (edge case) |
correction-invoice.json | Corrective Invoice | Preceding document references, type 384 |
credit-note.json | Credit Note | Negative amounts, credit note type |
Each example is available in GOBL JSON format and can be converted to UBL XML in any supported context.
Sources: test/data/convert/invoice-minimal.json1-138 test/data/convert/invoice-with-line-order.json1-178 test/data/convert/invoice-without-buyers-tax-id.json1-179 test/data/convert/correction-invoice.json1-149
The test framework provides several utility functions to simplify test implementation:
Test Helper Function Hierarchy
Sources: examples_test.go222-352 examples_test.go383-432
testInvoiceFrom(name string)
testInvoiceFromContext(name string, ctx ubl.Context)
loadTestEnvelopeFromPath(path string)
--update flag is settestParseInvoice(name string)
testLoadXML(name string)
The test framework includes optional integration with Phive, a validation service for UBL documents. When enabled via the --validate flag, each generated XML document is validated against its VESID (Validation Execution Set ID).
Phive Validation Flow
Sources: examples_test.go39-54 examples_test.go96-113
The test determines the appropriate VESID based on the document type (Invoice vs CreditNote) and the context's VESID configuration. Each context defines VESIDs for both document types:
The validation results are logged as JSON if validation fails, providing detailed error information for debugging.
Sources: examples_test.go103-112
The test framework includes an update mechanism to regenerate golden files when conversion logic changes intentionally.
The --update flag is defined as a boolean flag variable:
Sources: examples_test.go37
When --update is enabled:
out/ subdirectoriesThe update mechanism is used in three locations:
loadTestEnvelopeFromPath: Updates source GOBL files examples_test.go272-280TestConvertToInvoice: Updates UBL XML output examples_test.go91-94TestParseInvoice: Updates parsed GOBL JSON output examples_test.go181-189In addition to the comprehensive example tests, the library includes focused tests for specific components:
The party_test.go file validates party conversion details:
This test verifies that party endpoint IDs are correctly converted from GOBL to UBL format.
Sources: party_test.go1-20
The ordering_test.go file validates ordering reference conversion:
This test confirms that when no ordering information is present, the library generates a default "NA" order reference as required by some UBL profiles.
Sources: ordering_test.go1-21
The test framework includes a legacy XML validation function using libxml2:
This function is currently unused but provides XSD validation capabilities if needed. The Phive integration is the preferred validation method.
Sources: examples_test.go368-381
Refresh this wiki