Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
82f7e421
Commit
82f7e421
authored
Sep 29, 2020
by
Mark Lapierre
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Document when to use negative predicate methods in E2E tests
parent
7e6fd918
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
53 additions
and
0 deletions
+53
-0
doc/development/testing_guide/end_to_end/best_practices.md
doc/development/testing_guide/end_to_end/best_practices.md
+53
-0
No files found.
doc/development/testing_guide/end_to_end/best_practices.md
View file @
82f7e421
...
...
@@ -310,3 +310,56 @@ end
# Using native mouse click events in the case of a mask/overlay
click_element_coordinates
(
:title
)
```
## Ensure `expect` statements wait efficiently
In general, we use an
`expect`
statement to check that something _is_ as we expect it. For example:
```
ruby
Page
::
Project
::
Pipeline
::
Show
.
perform
do
|
pipeline
|
expect
(
pipeline
).
to
have_job
(
"a_job"
)
end
```
### Ensure `expect` checks for negation efficiently
However, sometimes we want to check that something is _not_ as we _don't_ want it to be. In other
words, we want to make sure something is absent. In such a case we should use an appropriate
predicate method that returns quickly, rather than waiting for a state that won't appear.
It's most efficient to use a predicate method that returns immediately when there is no job, or waits
until it disappears:
```
ruby
# Good
Page
::
Project
::
Pipeline
::
Show
.
perform
do
|
pipeline
|
expect
(
pipeline
).
to
have_no_job
(
"a_job"
)
end
```
### Problematic alternatives
Alternatively, if we want to check that a job doesn't exist it might be tempting to use
`not_to`
:
```
ruby
# Bad
Page
::
Project
::
Pipeline
::
Show
.
perform
do
|
pipeline
|
expect
(
pipeline
).
not_to
have_job
(
"a_job"
)
end
```
For this statement to pass,
`have_job("a_job")`
has to return
`false`
so that
`not_to`
can negate it.
The problem is that
`have_job("a_job")`
waits up to ten seconds for
`"a job"`
to appear before
returning
`false`
. Under the expected condition this test will take ten seconds longer than it needs to.
Instead, we could force no wait:
```
ruby
# Not as bad but potentially flaky
Page
::
Project
::
Pipeline
::
Show
.
perform
do
|
pipeline
|
expect
(
pipeline
).
not_to
have_job
(
"a_job"
,
wait:
0
)
end
```
The problem is that if
`"a_job"`
is present and we're waiting for it to disappear, this statement
will fail.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment