Skip to main content

Shell commands

DrevOps\BehatSteps\Steps\Web\CommandTrait - Source, Example, Helpers

Run local shell commands and assert on their result.

  • Run a shell command and capture its output, error output, and exit code.
  • Assert that the command succeeded or failed.
  • Assert the exit code, standard output, and error output.
  • Assert how long the command took to complete.

Commands run through the system shell with the privileges of the process that runs the tests. The command string is passed to the shell verbatim and is subject to shell expansion, so untrusted input must never be interpolated into it.

Options​

OptionTypeDefaultTagDescription
command.timeoutinteger300-Maximum time, in seconds, a command may run before it is terminated.

Steps​

@When I run the command :command

Run a shell command

When I run the command "php -v"
When I run the command "./vendor/bin/phpunit --version"
@Then the command should succeed

Assert that the last command succeeded

When I run the command "php -v"
Then the command should succeed
@Then the command should fail

Assert that the last command failed

When I run the command "phpcs --standard=NonExisting"
Then the command should fail
@Then the command exit code should be :code

Assert that the last command exited with a specific code

When I run the command "php -r 'exit(3);'"
Then the command exit code should be 3
@Then the command output should contain :value

Assert that the command output contains a string

When I run the command "echo hello"
Then the command output should contain "hello"
@Then the command output should not contain :value

Assert that the command output does not contain a string

When I run the command "echo hello"
Then the command output should not contain "goodbye"
@Then the command output should be :value

Assert that the command output equals a string

When I run the command "echo hello"
Then the command output should be "hello"
@Then the command error output should contain :value

Assert that the command error output contains a string

When I run the command "ls /nonexistent"
Then the command error output should contain "No such file"
@Then the command should complete in less than :seconds second(s)

Assert that the command completed in less than a number of seconds

When I run the command "echo hello"
Then the command should complete in less than 5 seconds
@Then the command should complete in more than :seconds second(s)

Assert that the command completed in more than a number of seconds

When I run the command "sleep 2"
Then the command should complete in more than 1 second