mirror of
https://github.com/beefproject/beef
synced 2026-06-08 13:15:56 +00:00
BeEF Test Suite
This directory contains the BeEF test suite using RSpec and SimpleCov for comprehensive testing and coverage reporting.
Setup
Prerequisites
- Ruby 3.0+
- Bundler
- All gems from
Gemfile
Configuration Files
spec/spec_helper.rb- Main test configuration.simplecov- Coverage configurationspec/support/- Test helpers and utilities
Running Tests
Basic Commands
# Run all tests (fast, no coverage)
bundle exec rake short
# or
bundle exec rspec spec/ --tag '~run_on_browserstack' --tag '~run_on_long_tests'
# Run specific component tests
bundle exec rspec spec/beef/core/
bundle exec rspec spec/beef/extensions/
bundle exec rspec spec/beef/modules/
Coverage Commands
# Complete coverage (recommended)
bundle exec rake coverage
# or
COVERAGE=all bundle exec rspec spec/ --tag '~run_on_browserstack' --tag '~run_on_long_tests'
# Component-specific coverage
bundle exec rake coverage_core # Core only
bundle exec rake coverage_modules # Modules only
bundle exec rake coverage_extensions # Extensions only
Legacy Commands (Still Supported)
# Old coverage commands still work
bundle exec rake coverage:modules
bundle exec rake coverage:all
bundle exec rake coverage_complete
Architecture
SimpleCov Configuration
- Environment-based: Uses
COVERAGE=core|modules|extensions|allenvironment variable - Grouped reporting: Separate groups for Core, Extensions, and Modules
- Filtered tracking: Only tracks relevant files based on focus area
- HTML reports: Generated in
coverage/directory
Test Organization
- Centralized config:
BeefTestConfigmodule provides test data instead of global constants - Component isolation: Each component (core/extensions/modules) has dedicated specs
- Branch coverage: Realistic test data for conditional logic testing
- Mock management: Proper mocking of external dependencies
Rake Tasks
- Clean separation: Base
spec*tasks vs coveragecoverage*tasks - Environment variables: Coverage controlled via
COVERAGEenv var - No sequential execution: Single test runs with proper filtering
- Backward compatibility: Old task names still work
Key Improvements
✅ Eliminated Global Constants
- Replaced
BRANCH_COVERAGEconstants with centralizedBeefTestConfigmodule - No more "already initialized constant" warnings
✅ Simplified Coverage Logic
- Cleaner filtering using
track_filesinstead of complexadd_filterlogic - Environment variable
COVERAGEinstead ofCOVERAGE_FOCUS
✅ Better Test Organization
- Centralized test configuration in
BeefTestConfig - Component-specific test data loading
- Reduced code duplication
✅ Cleaner Rake Tasks
- Single execution instead of sequential runs
- Proper environment variable usage
- Backward compatibility maintained
✅ Standard Patterns
- Uses
.simplecovconfig file (standard practice) - Follows RSpec best practices
- Better separation of concerns
Coverage Focus Areas
- core: Framework core functionality
- extensions: Extension modules
- modules: Command modules (main focus)
- all: Complete coverage across all areas
Troubleshooting
Common Issues
-
"already initialized constant" warnings
- Fixed by using centralized config instead of global constants
-
Low coverage percentages
- Use
COVERAGE=allfor complete coverage - Ensure realistic test data triggers conditional paths
- Use
-
Test failures
- Check that mocks are properly configured
- Verify test data matches module expectations
Debug Commands
# Run with debug output
bundle exec rspec --format documentation
# Run single test file
bundle exec rspec spec/beef/modules/browser/browser_spec.rb
# Check coverage report
open coverage/index.html
Contributing
When adding new tests:
- Use
BeefTestConfig.branch_coverage_for(:component)for branch test data - Add realistic datastore values that trigger conditional logic
- Mock external dependencies (database, network, etc.)
- Follow existing patterns for consistency