|
| 1 | +# Component/platform test configurations |
| 2 | + |
| 3 | +Each (new) component/platform must have tests. This enables our CI system to perform a test build of the |
| 4 | +component/platform to ensure it compiles without any errors. |
| 5 | + |
| 6 | +Tests should incorporate the new component/platform itself as well as all |
| 7 | +[automations](https://esphome.io/automations/actions) it implements. |
| 8 | + |
| 9 | +!!! note |
| 10 | + These are **compile tests only** and do not test functionality. |
| 11 | + |
| 12 | +### Overview |
| 13 | + |
| 14 | +The tests aim to test compilation of the code for each processor architecture: |
| 15 | + |
| 16 | +- Xtensa (ESP8266, original ESP32 and S-series) |
| 17 | +- RISC-V (ESP32 C-series) |
| 18 | +- ARM (RP2040) |
| 19 | + |
| 20 | +...and for each supported framework: |
| 21 | + |
| 22 | +- Arduino |
| 23 | +- [ESP-IDF](https://github.com/espressif/esp-idf/) |
| 24 | + |
| 25 | +There should be *at least one test* for each framework/architecture combination. We can probably go without saying it, |
| 26 | +but some framework/architecture combinations are simply not supported/possible, so tests for those are impossible and, |
| 27 | +as such, are (naturally) omitted. |
| 28 | + |
| 29 | +### General structure |
| 30 | + |
| 31 | +We try to structure the tests in a way so as to minimize repetition. Let's look at the `dht12` sensor platform as an |
| 32 | +example: |
| 33 | + |
| 34 | +First, you'll find a `common.yaml` file which contains this: |
| 35 | + |
| 36 | +```yaml |
| 37 | +i2c: |
| 38 | + - id: i2c_dht12 |
| 39 | + scl: ${scl_pin} |
| 40 | + sda: ${sda_pin} |
| 41 | + |
| 42 | +sensor: |
| 43 | + - platform: dht12 |
| 44 | + temperature: |
| 45 | + name: DHT12 Temperature |
| 46 | + humidity: |
| 47 | + name: DHT12 Humidity |
| 48 | + update_interval: 15s |
| 49 | +``` |
| 50 | +
|
| 51 | +It's a shared configuration file that defines common settings used across all hardware platforms. Having a "common" |
| 52 | +file like this minimizes duplication and ensures test consistency across all platforms. |
| 53 | +
|
| 54 | +To use `common.yaml` in a test configuration, YAML substitutions and the insertion operator are used (see |
| 55 | +[substitutions](https://esphome.io/components/substitutions)). This allows the test YAML file to reference and include |
| 56 | +the shared configuration. For the `dht12` platform, one of the test files is named `test.esp32-ard.yaml` and it contains |
| 57 | +this: |
| 58 | + |
| 59 | +```yaml |
| 60 | +substitutions: |
| 61 | + scl_pin: GPIO16 |
| 62 | + sda_pin: GPIO17 |
| 63 | +
|
| 64 | +<<: !include common.yaml |
| 65 | +``` |
| 66 | + |
| 67 | +By including `common.yaml`, all test configurations maintain the same structure while allowing flexibility for |
| 68 | +platform-specific substitutions such as pin assignments. This approach simplifies managing multiple test cases across |
| 69 | +different hardware platforms. |
| 70 | + |
| 71 | +### Which tests do I need? |
| 72 | + |
| 73 | +**We require a test for each framework/architecture combination** the component/platform supports. *Most* |
| 74 | +components/platforms include the following test files: |
| 75 | + |
| 76 | +- `test.esp32-ard.yaml` - ESP32 (Xtensa)/Arduino |
| 77 | +- `test.esp32-idf.yaml` - ESP32 (Xtensa)/IDF |
| 78 | +- `test.esp32-c3-ard.yaml` - ESP32-C3 (RISC-V)/Arduino |
| 79 | +- `test.esp32-c3-idf.yaml` - ESP32-C3 (RISC-V)/IDF |
| 80 | +- `test.esp8266-ard.yaml` - ESP8266 (Xtensa)/Arduino |
| 81 | +- `test.rp2040-ard.yaml` - RP2040 (ARM)/Arduino |
| 82 | + |
| 83 | +Because our test script checks for **successful compilation only**, it is not necessary to include a test for every |
| 84 | +microcontroller variant _unless different code is compiled based on the variant._ For example, the ESP32, ESP32-S2 |
| 85 | +and ESP32-S3 are all based on the same Xtensa architecture and, as such, use the same tooling and configuration. Given |
| 86 | +a block of code, if it compiles for the ESP32, it will compile for the S2 and S3, as well. Therefore, spinning up a new |
| 87 | +test run for each variant within this family is not productive and we avoid doing so to minimize CI runtime. |
| 88 | + |
| 89 | +If your component/platform builds different code based on the variant (for example, ESP32/S2/S3 or C3/C6), then tests |
| 90 | +should be added to/omitted from the list above as appropriate. This is often only necessary when implementing support |
| 91 | +for some specific hardware component built into the microcontroller. The |
| 92 | +[ADC](https://esphome.io/components/sensor/adc) is one example of this. |
| 93 | + |
| 94 | +### Running the tests |
| 95 | + |
| 96 | +You can run the tests locally simply by invoking the test script: |
| 97 | + |
| 98 | +```shell |
| 99 | +script/test_build_components -e compile -c dht12 |
| 100 | +``` |
| 101 | + |
| 102 | +Our CI will also run this script when you create or update your pull request (PR). |
0 commit comments