A PowerShell module designed to help convert structured data formats and generate realistic test data for Pester tests. PSTestableData provides utilities for working with JSON, YAML, and PowerShell objects, making it easy to create consistent and maintainable test data for your PowerShell projects.
Install-PSResource -Name PSTestableData -Scope CurrentUseror
Install-Module -Name PSTestableData -Scope CurrentUser- Convert JSON/YAML data to PowerShell objects and export as dot-sourceable .ps1 files
- Generate realistic test data by analyzing existing data patterns and structures
- Configure data generation behavior with editable configuration templates
- Preserve specific fields during anonymization for testing scenarios
- Support for complex nested structures, arrays, and mixed data types
- Anonymize sensitive data while maintaining structural integrity
- Fast pattern recognition and data generation for large datasets
Converts JSON or YAML data to PowerShell objects and writes them to a .ps1 file for dot-sourcing in tests.
Key Parameters:
InputObject- Raw JSON or YAML data as stringName- Base name for variables in output filePath- Output file path for the .ps1 fileFrom- Input format ('json' or 'yaml')Anonymize- Anonymize sensitive dataAsHashtable/AsPSCustomObject- Output format control
Analyzes a sample object and creates a configuration template that controls how data generation should work for each field.
Key Parameters:
SampleObject- The sample data to analyzeDefaultAction- Default action for all fields ('Preserve', 'Anonymize', 'Randomize')DefaultArrayCount- Default number of items for arraysAnonymizePatterns- Field patterns that should be anonymized by defaultOutputPath- Save configuration to a .ps1 file for editing and reusePassThru- Return configuration object when saving to file
Generates new test data using a configuration template created by New-ConfigurationFromSample.
Key Parameters:
ConfigPath- Path to configuration file (.ps1) created by New-ConfigurationFromSampleConfig- Configuration hashtable directly (alternative to ConfigPath)SeedData- Seed data for preserved/anonymized values (accepts pipeline input)Count- Number of data items to generateAsJson/AsYaml- Output format options
Analyzes structured data and generates new data following the same patterns and structure.
Key Parameters:
InputObject- Sample data to analyze and replicateCount- Number of data items to generateMaxArrayItems- Maximum items in generated arraysPreservePatterns- Field patterns to preserve during generationAnonymize- Enable data anonymization
Converts PowerShell objects to hashtable format, useful for data transformation and serialization.
Converts hashtables and other objects to PSCustomObject format for consistent object handling.
# Convert JSON data to a PowerShell script file
$jsonData = @'
{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": {
"name": "app-config",
"namespace": "default"
},
"data": {
"database.url": "postgresql://db:5432/app",
"cache.ttl": "300"
}
}
'@
ConvertTo-TestableData -InputObject $jsonData -Name "ConfigMap" -Path "./test-data.ps1" -From "json" -AsHashtableThis creates a test-data.ps1 file that you can dot-source in your tests:
# In your test file
. ./test-data.ps1
# Now $ConfigMap contains your converted data# Create sample data structure
$sampleData = @{
users = @(
@{
id = 1
name = "John Doe"
email = "john.doe@company.com"
active = $true
created = "2023-01-15T10:30:00Z"
}
)
metadata = @{
version = "1.0.0"
environment = "production"
}
}
# Generate 5 similar data structures
$testData = New-StructuredDataFromSample -InputObject $sampleData -Count 5 -MaxArrayItems 3
# Each item in $testData will have the same structure but different values# Step 1: Analyze sample data and create a configuration template
$sampleData = @{
apiVersion = "v1"
kind = "Namespace"
metadata = @{
name = "my-namespace"
labels = @{
"app.kubernetes.io/name" = "my-app"
}
}
spec = @{
finalizers = @("kubernetes")
}
}
# Create configuration with custom defaults
$config = New-ConfigurationFromSample -SampleObject $sampleData -DefaultAction "Randomize" -DefaultArrayCount 4
# Save configuration to file for editing
$configPath = New-ConfigurationFromSample -SampleObject $sampleData -OutputPath ".\namespace-config.ps1" -PassThru
# Edit the config file to customize field behaviors
# For example, change metadata.name to Action = "Preserve" to keep namespace names# Step 2: Use the configuration to generate test data
$seedData = @{
apiVersion = "v1"
kind = "Namespace"
metadata = @{
name = "production-namespace"
labels = @{
"app.kubernetes.io/name" = "web-app"
}
}
spec = @{
finalizers = @("kubernetes")
}
}
# Generate 3 test namespaces using the configuration and seed data
$testNamespaces = $seedData | New-DataFromConfiguration -ConfigPath ".\namespace-config.ps1" -Count 3
# Or use configuration hashtable directly
$testNamespaces = $seedData | New-DataFromConfiguration -Config $config -Count 3
# Each generated namespace will:
# - Preserve apiVersion and kind from seed
# - Randomize metadata.name (unless you edited config to preserve it)
# - Generate appropriate random values for other fields
# - Use ArrayCount=4 for any arrays in the structure$sensitiveData = @{
apiVersion = "v1" # Keep this unchanged
kind = "Secret" # Keep this unchanged
metadata = @{
name = "db-credentials" # This will be anonymized
namespace = "production" # This will be anonymized
}
data = @{
username = "admin" # This will be anonymized
password = "secret123" # This will be anonymized
}
}
# Preserve Kubernetes API fields but anonymize data
$preservePatterns = @("apiVersion", "kind", "metadata.namespace")
$anonymizedData = New-StructuredDataFromSample -InputObject $sensitiveData -Anonymize -PreservePatterns $preservePatterns$yamlData = @'
apiVersion: apps/v1
kind: Deployment
spec:
replicas: 3
selector:
matchLabels:
app: web-server
template:
spec:
containers:
- name: web
image: nginx:1.20
ports:
- containerPort: 80
env:
- name: ENV
value: "production"
- name: sidecar
image: logging:1.0
ports:
- containerPort: 8080
'@
ConvertTo-TestableData -InputObject $yamlData -Name "Deployment" -Path "./k8s-test-data.ps1" -From "yaml" -AsPSCustomObject# Generate test data for different scenarios
$baseConfig = @{
server = @{
host = "localhost"
port = 8080
ssl = $false
}
features = @("auth", "logging", "metrics")
limits = @{
maxConnections = 100
timeout = 30
}
}
# Generate 10 different configurations for testing
$testConfigs = 1..10 | ForEach-Object {
New-StructuredDataFromSample -InputObject $baseConfig -MaxArrayItems 5
}
# Use in Pester tests
Describe "Server Configuration Tests" {
It "Should handle configuration <_>" -ForEach (1..10) {
$config = $testConfigs[$_ - 1]
# Test your function with different configurations
Test-ServerConfig -Config $config | Should -Not -Throw
}
}$complexData = @{
application = @{
name = "MyApp"
version = "2.1.0"
components = @(
@{
type = "web"
instances = 3
config = @{
memory = "512Mi"
cpu = "200m"
}
},
@{
type = "database"
instances = 1
config = @{
memory = "1Gi"
cpu = "500m"
storage = "10Gi"
}
}
)
monitoring = @{
enabled = $true
endpoints = @("/health", "/metrics", "/ready")
}
}
}
# Generate test data maintaining the complex structure
$testApplications = New-StructuredDataFromSample -InputObject $complexData -Count 3 -MaxArrayItems 4- Create consistent test data files that can be version controlled
- Generate multiple test scenarios from a single sample
- Maintain test data structure while anonymizing sensitive information
- Use configuration templates to control data generation behavior
- Generate realistic test datasets for integration tests
- Create reproducible test scenarios across different environments
- Validate application behavior with varied but structured data
- Customize data generation rules for different testing environments
- Create sample data that matches production structures
- Generate edge cases and boundary conditions for testing
- Prototype with realistic data before production deployment
- Edit configuration templates to fine-tune data generation rules
We welcome contributions! Please see our Contributing Guidelines for details on:
- Reporting bugs and requesting features
- Setting up the development environment
- Running tests and submitting pull requests
- PowerShell 5.1 or later
- PowerShell Core 6.0+ (cross-platform support)
This project is licensed under the MIT License - see the LICENSE file for details.