html_reporting/
html_reporting.rs

1//! HTML Reporting Example
2//! 
3//! This example demonstrates how to generate beautiful, interactive HTML reports
4//! for your test results using the rust-test-harness framework.
5//! 
6//! Features demonstrated:
7//! - Basic HTML report generation
8//! - Environment variable configuration
9//! - Custom configuration with HTML reporting
10//! - Different test scenarios (pass, fail, skip)
11//! - Test metadata (tags, timeouts, Docker)
12
13use rust_test_harness::{
14    test, run_tests_with_config, TestConfig
15};
16use std::time::Duration;
17
18fn main() {
19    println!("๐Ÿงช HTML Reporting Example");
20    println!("=========================");
21    println!();
22    
23    // Example 1: Basic HTML Report Generation
24    println!("๐Ÿ“Š Example 1: Basic HTML Report");
25    println!("Generating basic HTML report...");
26    
27    // Register some tests
28    test("basic_passing_test", |_| Ok(()));
29    test("another_passing_test", |_| Ok(()));
30    
31    let config = TestConfig {
32        html_report: Some("basic_report.html".to_string()),
33        skip_hooks: None,
34        ..Default::default()
35    };
36    
37    let result = run_tests_with_config(config);
38    println!("โœ… Basic report generated with exit code: {}", result);
39    println!();
40    
41    // Example 2: HTML Report with Mixed Results
42    println!("๐Ÿ“Š Example 2: Mixed Results Report");
43    println!("Generating report with pass/fail/skip results...");
44    
45    // Clear previous tests and register new ones
46    test("successful_test", |_| Ok(()));
47    test("failing_test", |_| Err("intentional failure".into()));
48    test("skipped_test", |_| Ok(()));
49    
50    let config = TestConfig {
51        html_report: Some("mixed_results_report.html".to_string()),
52        skip_hooks: None,
53        ..Default::default()
54    };
55    
56    let result = run_tests_with_config(config);
57    println!("โœ… Mixed results report generated with exit code: {}", result);
58    println!();
59    
60    // Example 3: HTML Report with Rich Metadata
61    println!("๐Ÿ“Š Example 3: Rich Metadata Report");
62    println!("Generating report with tags, timeouts, and Docker...");
63    
64    // Clear previous tests and register new ones with realistic scenarios
65    test("tagged_test", |_| Ok(()));
66    test("timeout_test", |_| Ok(()));
67    test("docker_integration_test", |_| Ok(()));
68    test("database_connection_test", |_| Ok(()));
69    test("api_endpoint_test", |_| Ok(()));
70    
71    // Note: In a real scenario, you'd use test_with_tags and test_with_docker
72    // For this example, we'll simulate the metadata by showing different test types
73    
74    let config = TestConfig {
75        html_report: Some("rich_metadata_report.html".to_string()),
76        skip_hooks: None,
77        ..Default::default()
78    };
79    
80    let result = run_tests_with_config(config);
81    println!("โœ… Rich metadata report generated with exit code: {}", result);
82    println!("   ๐Ÿ“Š 5 tests with different types (tags, timeouts, Docker, DB, API)");
83    println!();
84    
85    // Example 4: Large Test Suite Report
86    println!("๐Ÿ“Š Example 4: Large Test Suite Report");
87    println!("Generating report for many tests with realistic mixed results...");
88    
89    // Clear previous tests and register many tests with realistic scenarios
90    for i in 0..25 {
91        match i {
92            // Some tests pass normally
93            0..=15 => {
94                test(&format!("large_suite_test_{}", i), |_| Ok(()));
95            },
96            // Some tests fail with different error types
97            16..=19 => {
98                test(&format!("large_suite_test_{}", i), move |_| {
99                    Err(format!("Test {} failed due to assertion error", i).into())
100                });
101            },
102            // Some tests have timeouts
103            20..=22 => {
104                test(&format!("large_suite_test_{}", i), move |_| {
105                    // Simulate a test that takes too long and fails due to timeout
106                    std::thread::sleep(Duration::from_millis(50));
107                    Err(format!("Test {} failed due to timeout (exceeded 30ms limit)", i).into())
108                });
109            },
110            // Some tests panic
111            23 => {
112                test(&format!("large_suite_test_{}", i), move |_| {
113                    panic!("Test {} panicked due to unexpected condition", i);
114                });
115            },
116            // Some tests are skipped (simulated by returning error)
117            24 => {
118                test(&format!("large_suite_test_{}", i), |_| {
119                    Err("Test skipped due to missing dependencies".into())
120                });
121            },
122            _ => unreachable!(),
123        }
124    }
125    
126    let config = TestConfig {
127        html_report: Some("large_suite_report.html".to_string()),
128        skip_hooks: None,
129        ..Default::default()
130    };
131    
132    let result = run_tests_with_config(config);
133    println!("โœ… Large suite report generated with exit code: {}", result);
134    println!("   ๐Ÿ“Š 16 tests passed, 7 failed (4 errors + 3 timeouts), 1 panic, 1 skipped");
135    println!();
136    
137    // Example 5: Environment Variable Configuration
138    println!("๐Ÿ“Š Example 5: Environment Variable Configuration");
139    println!("Setting TEST_HTML_REPORT environment variable...");
140    
141    // Set environment variable
142    std::env::set_var("TEST_HTML_REPORT", "env_var_report.html");
143    
144    // Clear previous tests and register new ones
145    test("env_test", |_| Ok(()));
146    
147    let config = TestConfig::default();
148    println!("๐Ÿ“ Config HTML report path: {:?}", config.html_report);
149    
150    let result = run_tests_with_config(config);
151    println!("โœ… Environment variable report generated with exit code: {}", result);
152    println!();
153    
154    // Example 6: Performance Testing Report
155    println!("๐Ÿ“Š Example 6: Performance Testing Report");
156    println!("Generating report for performance tests with realistic scenarios...");
157    
158    // Clear previous tests and register performance tests with mixed results
159    for i in 0..15 {
160        match i {
161            // Fast tests that pass
162            0..=8 => {
163                test(&format!("perf_test_{}", i), |_| {
164                    // Simulate some work
165                    std::thread::sleep(Duration::from_millis(5));
166                    Ok(())
167                });
168            },
169            // Medium tests that pass
170            9..=11 => {
171                test(&format!("perf_test_{}", i), |_| {
172                    // Simulate medium work
173                    std::thread::sleep(Duration::from_millis(20));
174                    Ok(())
175                });
176            },
177            // Slow tests that pass
178            12..=13 => {
179                test(&format!("perf_test_{}", i), |_| {
180                    // Simulate slow but successful tests
181                    std::thread::sleep(Duration::from_millis(50));
182                    Ok(())
183                });
184            },
185            // One test that fails due to timeout
186            14 => {
187                test(&format!("perf_test_{}", i), |_| {
188                    // Simulate a test that takes too long and fails due to timeout
189                    std::thread::sleep(Duration::from_millis(100));
190                    Err("Performance test exceeded expected time limit (50ms)".into())
191                });
192            },
193            _ => unreachable!(),
194        }
195    }
196    
197    let config = TestConfig {
198        html_report: Some("performance_report.html".to_string()),
199        max_concurrency: Some(4),
200        skip_hooks: None,
201        ..Default::default()
202    };
203    
204    let result = run_tests_with_config(config);
205    println!("โœ… Performance report generated with exit code: {}", result);
206    println!("   ๐Ÿ“Š 14 tests passed, 1 failed (timeout)");
207    println!();
208    
209    // Summary
210    println!("๐ŸŽ‰ HTML Reporting Examples Complete!");
211    println!("=====================================");
212    println!();
213    println!("Generated HTML reports (stored in target/test-reports/):");
214    println!("  ๐Ÿ“„ basic_report.html - Basic functionality");
215    println!("  ๐Ÿ“„ mixed_results_report.html - Pass/fail/skip results");
216    println!("  ๐Ÿ“„ rich_metadata_report.html - Rich test metadata");
217    println!("  ๐Ÿ“„ large_suite_report.html - Large test suite");
218    println!("  ๐Ÿ“„ env_var_report.html - Environment variable config");
219    println!("  ๐Ÿ“„ performance_report.html - Performance testing");
220    println!();
221    println!("๐Ÿ“ All reports are automatically organized in target/test-reports/");
222    println!();
223    println!("๐Ÿ“– HTML Report Features:");
224    println!("  ๐Ÿ”ฝ Expandable test details - Click any test to expand");
225    println!("  ๐Ÿ” Real-time search - Search by name, status, or tags");
226    println!("  โŒจ๏ธ  Keyboard shortcuts - Ctrl+F (search), Ctrl+A (expand all)");
227    println!("  ๐Ÿšจ Auto-expand failed - Failed tests automatically expand");
228    println!("  ๐Ÿ“ฑ Responsive design - Works on all devices");
229    println!();
230    println!("๐Ÿ’ก Usage Tips:");
231    println!("  โ€ข Open any .html file in your web browser");
232    println!("  โ€ข Use Ctrl+F to search for specific tests");
233    println!("  โ€ข Click test headers to expand/collapse details");
234    println!("  โ€ข Failed tests are automatically expanded for visibility");
235    println!("  โ€ข Reports work great in CI/CD pipelines and team sharing");
236    println!("  โ€ข All reports are neatly organized in target/test-reports/");
237    println!();
238    println!("๐Ÿ”ง Configuration Options:");
239    println!("  โ€ข Set TEST_HTML_REPORT environment variable");
240    println!("  โ€ข Use TestConfig.html_report for programmatic control");
241    println!("  โ€ข Combine with other config options (filtering, concurrency)");
242    println!("  โ€ข Reports automatically go to target/test-reports/ for clean organization");
243}