How to use the command "pio test" (with examples)
This article will illustrate various use cases of the command “pio test” in PlatformIO.
Command Description
The “pio test” command is used to run local tests on a PlatformIO project. It allows you to quickly test your project code within different environments and configurations. The command supports various options and arguments to provide flexibility in running the tests.
Use case 1: Run all tests in all environments of the current PlatformIO project
Code:
pio test
Motivation: The motivation for using this example is to run all the tests written in the project across all environments. This can be useful to ensure that the code works as expected in different contexts.
Explanation: This command runs all the tests in all environments of the current PlatformIO project. It does not require any additional arguments or options.
Example Output:
[RESULT] PASSES
Use case 2: Test only specific environments
Code:
pio test --environment environment1 --environment environment2
Motivation: Sometimes, it is necessary to run tests only in specific environments to validate the functionality of the code in those particular setups. This example allows you to do so.
Explanation: Using the “–environment” option, you can specify the environments in which you want to run the tests. The command will only run the tests in those specified environments.
Example Output:
[RESULT] PASSES
Use case 3: Run only tests whose name matches a specific glob pattern
Code:
pio test --filter "pattern"
Motivation: When dealing with a large number of tests, it can be beneficial to selectively run only the tests that match a specific pattern. This example demonstrates how to achieve that.
Explanation: The “–filter” option allows you to specify a pattern (using glob syntax) to match test names. Only the tests whose names match the provided pattern will be executed.
Example Output:
[RESULT] PASSES
Use case 4: Ignore tests whose name matches a specific glob pattern
Code:
pio test --ignore "pattern"
Motivation: In certain scenarios, you may want to exclude specific tests from running. This example showcases how you can achieve that by using the “–ignore” option.
Explanation: The “–ignore” option enables you to provide a pattern (using glob syntax) to match test names. The tests whose names match the pattern will be ignored and not executed.
Example Output:
[RESULT] PASSES
Use case 5: Specify a port for firmware uploading
Code:
pio test --upload-port upload_port
Motivation: If your testing process involves firmware uploading to a specific port, you can use this example to specify the port to which the firmware should be uploaded.
Explanation: By using the “–upload-port” option, you can provide the upload port to which the firmware should be uploaded during the testing process. This is typically required when testing projects that communicate with external hardware.
Example Output:
[RESULT] PASSES
Use case 6: Specify a custom configuration file for running the tests
Code:
pio test --project-conf path/to/platformio.ini
Motivation: In some cases, you may want to use a custom configuration file for running tests instead of the default “platformio.ini” file. This example demonstrates how to specify a custom configuration file.
Explanation: The “–project-conf” option allows you to specify the path to a custom configuration file for running the tests. This can be helpful when you want to override certain settings or configurations specific to the testing environment.
Example Output:
[RESULT] PASSES
Conclusion
The “pio test” command provides a flexible and powerful way to run local tests on a PlatformIO project. By utilizing the various options and arguments provided by the command, you can customize your testing process to suit your project requirements. Whether you want to run tests in specific environments, selectively execute tests based on pattern matching, or specify custom configurations, the “pio test” command has got you covered.