Skip to content

test: improve code coverage from 94.3% to 98.8%#46

Merged
miclle merged 7 commits intofox-gonic:mainfrom
miclle:test/improve-coverage
Dec 8, 2025
Merged

test: improve code coverage from 94.3% to 98.8%#46
miclle merged 7 commits intofox-gonic:mainfrom
miclle:test/improve-coverage

Conversation

@miclle
Copy link
Copy Markdown
Member

@miclle miclle commented Dec 8, 2025

Summary

This PR significantly improves the test coverage of the fox framework from 94.3% to 98.8%.

Changes

Coverage Improvements by Package

  • github.com/fox-gonic/fox: 94.3% → 98.8% (+4.5%)
  • github.com/fox-gonic/fox/logger: 90.8% → 91.8% (+1.0%)
  • github.com/fox-gonic/fox/httperrors: 96.8% (maintained)
  • github.com/fox-gonic/fox/utils: 100.0% (maintained)

Key Improvements

1. binding.go - bind() function (93.2% → 94.9%)

  • Add TestBind_CustomBinder for custom binder error path
  • Add TestBind_ContextFieldNotConvertible for type conversion edge cases
  • Add TestBind_WithBindingError for JSON parsing errors
  • Add TestBind_DefaultBinder tests for fallback binding scenarios
  • Add TestBind_BodyBinder tests for empty body handling
  • Add TestBind_PointerToPointer for nested pointer types
  • Add TestBind_NonStructTarget for non-struct binding
  • Add TestBind_RequestBodyError for body read errors

2. httperrors/errors.go - MarshalJSON() (92.3% → 96.8%)

  • Add TestMarshalJSON_MetaNil for nil Meta field
  • Add TestMarshalJSON_CodeEmpty for empty Code field
  • Add TestMarshalJSON_ErrorEmpty for empty error message
  • Add TestMarshalJSON_MetaCodeInStruct for code in Meta struct
  • Add TestMarshalJSON_MetaErrorInStruct for error in Meta struct
  • Add TestMarshalJSON_MetaWithMap for Meta with map values
  • Add TestMarshalJSON_MetaWithPrimitiveType for Meta with primitive types

3. logger/logger.go - newLogger() (94.7% → 100%)

  • Add TestNewLogger_FileLoggingHumanReadable for file logging with human-readable format

4. Other improvements

  • Add debug_test.go with comprehensive debug mode tests
  • Add context_test.go tests for RequestBody edge cases
  • Add render_test.go tests for renderError with render.Render interface
  • Add engine_test.go tests for SetMode/Mode and CORS configuration
  • Add routergroup_test.go tests for preset logger and trace ID scenarios

Test Results

All tests pass successfully:

make test

Remaining Uncovered Code

The remaining uncovered code consists of:

  • Fatal/Fatalf functions in logger package (cannot be safely tested as they exit the program)
  • Panic/Panicf functions in logger package (not recommended to test)
  • Error handling paths in bind() that are extremely difficult to trigger (Query/URI/Header binding errors)

These represent edge cases that are either unsafe to test or nearly impossible to trigger in practice.

Checklist

  • All tests pass
  • Coverage improved significantly
  • No breaking changes
  • Focused on testing real-world scenarios and edge cases

This commit enhances test coverage by adding comprehensive tests for:

- SetMode() and Mode() functions - Test all mode values (debug, release, test)
- CORS() method - Test valid/invalid configurations and wildcard origins
- RequestBody() method - Add edge case for non-[]byte cached values
- bind() function - Test DefaultBinder and bodyBinders with empty body scenarios
- queryBinding.Bind() - Test nil validator scenario
- httperrors.MarshalJSON() - Test nil Meta, empty Code, and empty error scenarios

Coverage improvements:
- Main package: 94.3% → 96.1% (+1.8%)
- All tests pass with improved edge case handling
This commit adds comprehensive tests to further enhance coverage:

- Add debug_test.go for debugPrint and debugPrintRoute functions
- Add bind() test for RequestBodyError and non-struct targets
- Add renderError() test for render.Render interface errors
- Add handleWrapper() tests for preset logger and trace ID scenarios

Coverage improvements:
- Main package: 96.1% → 97.9% (+1.8%)
- bind() function: 88.1% → 91.5%
- renderError() function: 87.5% → 100%

Total improvement: 94.3% → 97.9% (+3.6%)
This commit adds additional edge case tests:

- queryBinding.Bind() - Add validation error and MapFormWithTag error tests
- bind() function - Add DefaultBinder as regular Binding test (93.2%)
- httperrors.MarshalJSON() - Add tests for Meta with code/error fields

Coverage improvements:
- Main package: 97.9% → 98.5% (+0.6%)
- bind() function: 91.5% → 93.2%

Total improvement: 94.3% → 98.5% (+4.2%)
- Remove invalid validation error test from queryBinding.Bind
- Simplify TestRenderError_RenderInterface to avoid Content-Type assertion

All tests now pass with make test
Improvements:
- Add TestBind_CustomBinder for custom binder error path (bind.go:63-65)
- Add TestBind_ContextFieldNotConvertible for type conversion edge case
- Add TestBind_WithBindingError for JSON parsing error
- Add TestMarshalJSON_MetaWithMap for Meta with map values
- Add TestMarshalJSON_MetaWithPrimitiveType for Meta with primitive types

These changes bring bind() coverage from 93.2% to 94.9% and improve
overall test robustness for edge cases.
Add TestNewLogger_FileLoggingHumanReadable to test file logging
with human-readable (non-JSON) format. This covers the previously
untested branch in newLogger() function at lines 132-134.

Coverage improvements:
- logger/logger.go newLogger(): 94.7% -> 100%
- logger package overall: 90.8% -> 91.8%
Fix golangci-lint testifylint error by using require.NoError
instead of assert.NoError for error assertions.
@codecov
Copy link
Copy Markdown

codecov Bot commented Dec 8, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.00%. Comparing base (56ab157) to head (1d95a29).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main      #46      +/-   ##
==========================================
+ Coverage   91.87%   96.00%   +4.13%     
==========================================
  Files          16       16              
  Lines         726      726              
==========================================
+ Hits          667      697      +30     
+ Misses         42       22      -20     
+ Partials       17        7      -10     
Flag Coverage Δ
unittests 96.00% <ø> (+4.13%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.
see 6 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@miclle miclle merged commit 313aeab into fox-gonic:main Dec 8, 2025
10 checks passed
@miclle miclle deleted the test/improve-coverage branch December 8, 2025 09:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant