4.7 KiB
ESP32 Dimmer Driver Unit Tests
This directory contains unit tests for the ESP32 Dimmer Driver library.
Test Structure
The tests are organized using the ESP-IDF Unity testing framework and cover the following functionality:
Test Categories
-
Dimmer Creation Tests
test_createDimmer_returns_valid_pointer: Verifies that createDimmer returns a valid pointertest_createDimmer_with_different_pins: Tests creating multiple dimmers with different GPIO pins
-
Power Control Tests
test_setPower_getPower_normal_values: Tests setting and getting power with normal values (0, 50, 99)test_setPower_boundary_values: Tests boundary conditions (values above 99 are clamped)test_getPower_when_dimmer_off: Verifies getPower returns 0 when dimmer is OFF
-
State Management Tests
test_setState_getState: Tests setting and getting the dimmer state (ON/OFF)test_changeState_toggles_state: Verifies changeState toggles between ON and OFFtest_state_affects_getPower: Tests that state changes affect getPower behavior
-
Mode Management Tests
test_setMode_getMode: Tests setting and getting dimmer modes (NORMAL/TOGGLE)test_toggleSettings_with_valid_values: Tests toggle settings with valid rangetest_toggleSettings_boundary_values: Tests toggle settings with boundary values
-
Initialization Tests
test_begin_with_normal_mode: Tests initialization with NORMAL_MODEtest_begin_with_toggle_mode: Tests initialization with TOGGLE_MODE
-
Multiple Dimmer Tests
test_multiple_dimmers_independent: Verifies multiple dimmers operate independently
Running the Tests
Prerequisites
- ESP-IDF v5.0 or higher installed
- ESP32 development board (tests can run without actual hardware connected)
Validate Your Setup
Before running tests, you can validate your environment:
./validate_setup.sh
This will check:
- Python and ESP-IDF installation
- Required files and directories
- Serial port availability
- CMake installation
Build and Flash
-
Navigate to the test_app directory:
cd test_app -
Set the target (if not already set):
idf.py set-target esp32 -
Build the test application:
idf.py build -
Flash to your ESP32 board:
idf.py -p /dev/ttyUSB0 flash monitorReplace
/dev/ttyUSB0with your serial port.
Expected Output
When the tests run successfully, you should see output similar to:
========================================
ESP32 Dimmer Driver Unit Tests
========================================
Running test_createDimmer_returns_valid_pointer...
PASS
Running test_createDimmer_with_different_pins...
PASS
...
========================================
All tests completed!
========================================
16 Tests 0 Failures 0 Ignored
OK
Test Coverage
The unit tests cover:
- ✅ Dimmer object creation
- ✅ Power setting and retrieval with boundary conditions
- ✅ State management (ON/OFF)
- ✅ Mode management (NORMAL/TOGGLE)
- ✅ Toggle settings configuration
- ✅ Multiple independent dimmers
- ✅ State-dependent behavior
Notes for Future Refactoring
These unit tests are designed to:
-
Enable safe refactoring: The tests verify the API contract, allowing you to refactor internal implementation while ensuring the public API behavior remains consistent.
-
Document expected behavior: Each test serves as executable documentation of how the library should behave.
-
Catch regressions: Running these tests after changes helps catch any unintended behavior changes.
-
Hardware independence: These tests focus on the software logic and can run without actual dimmer hardware connected. They test the API layer and state management.
Limitations
- These tests do not verify actual hardware functionality (zero-crossing detection, TRIAC firing)
- ISR behavior is not directly tested (requires hardware/simulation)
- Timing-critical code paths are not fully tested without hardware
For hardware integration testing, additional tests would be needed with actual hardware or simulation.
Adding New Tests
To add new tests:
- Add new test functions to
main/test_main.c - Use the Unity assertion macros (TEST_ASSERT_*, etc.)
- Register the test with
RUN_TEST()in theapp_main()function - Follow the naming convention:
test_<functionality>_<scenario>
Example:
void test_new_feature_basic_functionality(void)
{
dimmertyp *dimmer = createDimmer(TEST_TRIAC_GPIO, TEST_ZC_GPIO);
TEST_ASSERT_NOT_NULL(dimmer);
// Your test code here
}
Then add to app_main():
RUN_TEST(test_new_feature_basic_functionality);