Chapter 7: Skipping Tests in PestPHP
In this chapter, we'll explore how PestPHP allows you to skip tests temporarily. There are situations during the development process when you might need to disable specific tests, and PestPHP provides a straightforward way to achieve this without removing the test code.
Understanding Test Skipping
Skipping tests in PestPHP allows you to temporarily exclude certain tests from the test suite. This can be useful when you're working on specific parts of your code, and running certain tests might be unnecessary or problematic during a particular development phase.
Basic Test Skipping
To skip a test, use the skip
function:
test('this test is temporarily skipped')
->skip()
->test(function () {
// Test logic goes here
});
In this example, the skip
function is used to mark the test as skipped. PestPHP will ignore the test during test execution.
Skipping Tests Conditionally
You can also skip tests based on a condition using the skipIf
function:
test('skip this test conditionally')
->skipIf(PHP_VERSION_ID < 80000)
->test(function () {
// Test logic goes here
});
In this case, the test will be skipped if the condition (PHP_VERSION_ID < 80000
) evaluates to true
. This allows you to skip tests based on specific runtime conditions.
Practical Use Cases
Incomplete Features
When working on a feature that is not yet complete, you can skip tests related to that feature:
test('feature functionality is pending')
->skip()
->test(function () {
// Feature-related test logic goes here
});
Known Issues
If a test is known to be failing due to a known issue that hasn't been addressed yet, you can skip the test until the issue is resolved:
test('known issue with this test')
->skip('Known issue: Bug #12345')
->test(function () {
// Test logic goes here
});
Conclusion
Skipping tests in PestPHP provides a convenient way to temporarily exclude specific tests during the development process. This feature allows you to focus on the tasks at hand without being distracted by tests that are not relevant or are known to be problematic. By using skip
and skipIf
, you can control which tests are executed, making your development workflow more efficient. In the next chapter, we will explore PestPHP's extensibility through custom macros, enabling you to tailor PestPHP to the specific needs of your project.
Subscribe to my newsletter
Read articles from Tresor Kasenda directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Tresor Kasenda
Tresor Kasenda
I've been a senior web developer for over seven years. I help build high-level web applications using technologies like PHP and Python. I love to share my knowledge with the community and help those who want to improve their skills in software development on technologies like Laravel, Livewire, AlpineJs, VueJs, and Django.