Skip to content

Commit 1c54143

Browse files
author
Dagger
committed
add_test_implementation_1766933675
1 parent 43011b2 commit 1c54143

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

TESTING.md

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# Test Documentation
2+
3+
This document describes the comprehensive testing approach for the Greetings API project.
4+
5+
## Overview
6+
7+
The Greetings API project includes multiple layers of testing to ensure reliability and correctness:
8+
- Unit tests for backend logic
9+
- End-to-end tests for the frontend
10+
- Linting for code quality
11+
12+
## Backend Tests
13+
14+
The backend tests are located in `main_test.go` and use the `gotest.tools/v3` testing framework.
15+
16+
### Test Functions
17+
18+
#### TestSelectGreeting
19+
Tests the core greeting selection logic with multiple scenarios:
20+
- **Valid language selection**: Tests retrieval of greetings by specific language (e.g., "english")
21+
- **Random selection**: Tests the random greeting functionality
22+
- **Invalid language handling**: Verifies proper error handling for non-existent languages
23+
- **Empty language handling**: Ensures errors are returned for empty language strings
24+
25+
#### TestFormatResponse
26+
Tests the JSON response formatting:
27+
- Verifies the greeting is properly formatted as JSON
28+
- Ensures the structure matches the expected API response format: `{"greeting":"<message>"}`
29+
30+
### Test Coverage
31+
32+
Current test coverage includes:
33+
- ✅ Greeting selection by language
34+
- ✅ Random greeting selection
35+
- ✅ Error handling for invalid languages
36+
- ✅ Error handling for empty language strings
37+
- ✅ Response formatting
38+
- ✅ Empty greetings slice handling
39+
40+
## Frontend Tests
41+
42+
End-to-end tests for the frontend are located in `website/cypress/e2e/greeting_test.cy.ts` using Cypress.
43+
44+
### Test Scenarios
45+
46+
1. **Page Load Test**: Verifies the page loads correctly with the expected header
47+
2. **Greeting Button Test**: Verifies clicking the greeting button changes the displayed message
48+
49+
## Running Tests
50+
51+
### All Tests (Recommended)
52+
```bash
53+
dagger call check
54+
```
55+
56+
This runs:
57+
- Backend linting (golangci-lint)
58+
- Backend unit tests
59+
- Frontend E2E tests
60+
- Frontend linting
61+
62+
### Backend Tests Only
63+
```bash
64+
# Using Dagger
65+
dagger call test
66+
67+
# Or directly with Go
68+
go test ./...
69+
```
70+
71+
### Frontend Tests Only
72+
```bash
73+
cd website
74+
npm run test:e2e
75+
```
76+
77+
### Linting Only
78+
```bash
79+
# Backend
80+
dagger call lint
81+
82+
# Frontend (part of check command)
83+
dagger call check
84+
```
85+
86+
## Test Data
87+
88+
Greetings test data is embedded from `greetings.json` which includes:
89+
- 17 different language greetings
90+
- Languages: English, British, French, Italian, Spanish, German, Mandarin, Hindi, Arabic, Bengali, Russian, Portuguese, Urdu, Indonesian, Japanese, Marathi, Telugu
91+
92+
## Continuous Integration
93+
94+
Tests are automatically run in CI through Dagger modules. The CI ensures:
95+
- All tests pass before merging
96+
- Code meets linting standards
97+
- No regressions are introduced
98+
99+
## Future Test Improvements
100+
101+
Potential areas for additional testing coverage:
102+
- HTTP handler integration tests (testing the actual HTTP endpoints with httptest)
103+
- Additional edge cases for error handling
104+
- Performance/load testing for the API
105+
- Testing CORS functionality
106+
- Testing with empty greetings.json
107+
- Concurrency testing for random selection
108+
109+
## Test Best Practices
110+
111+
When adding new tests:
112+
1. Follow Go testing conventions
113+
2. Use descriptive test function names
114+
3. Test both happy paths and error cases
115+
4. Keep tests isolated and independent
116+
5. Run `dagger call check` before committing
117+
6. Update this documentation when adding new test scenarios

0 commit comments

Comments
 (0)