Charlson So
Charlson So

Reputation: 81

What is a pending test in Rspec?

What is the use of a pending test in RSpec?

ex:

xit "it is named Ash" do

or

it "it is named Ash"

Thank you!

Upvotes: 6

Views: 9386

Answers (2)

Jeff Widman
Jeff Widman

Reputation: 23472

As a test input, pending() refers to tests that run, but are expected to fail. In fact a pending() test that suddenly passes will fail the test run. The ideal use case is if you run tests against external APIs that suddenly stop working for a day or two, you can mark as pending to unblock CI but still notify you when you can re-enable them.

This is in contrast to skip() which causes the test to be entirely skipped without running. For example if you've got a particular test that passes, but takes a long time to run, you may want to only run locally when working on that area of code, but mark as skip() so that it's ommitted from CI.

Confusingly, both "skipped" and "pending" tests are printed with a pending output:

# test marked as skipped

1 example, 0 failures, 1 pending

In general, the concept of pending as a test that is still run is relatively uncommon in other language testing ecosystems. Between that and the overloading of the term "pending" for the test outcome, it's no wonder people get confused and think that pending() and skipped() are the same!

Upvotes: 1

ndnenkov
ndnenkov

Reputation: 36101

The idea is to disable an example temporarily. Pending examples are not executed and are marked as pending.

One use case for this is if you want to run an entire spec except one or two examples. Instead of commenting them out, you can mark them as pending. Another reason is to write specs for something you know has to get implemented, but currently isn't.

In the execution report you will see

x examples, y failures, z pending


To mark an example as pending, you can either put an x in front of the it, as you have found out, or put in the place that you want to be pending:

pending 'some reason'

If you do the latter, the code in that example will be executed, but the example will count as pending instead of a success or failure.

Examples that don't have a body (do - end block) are also considered pending.

Upvotes: 14

Related Questions