If you're using Specsheet as part of a build pipeline for a server, you'll need to manage running the server in the background while Specsheet tests the running process by sending requests to it. The pipeline usually goes like this:
- Start the server process in the background.
- Wait a few seconds for it to become available.
- Run all the tests.
- Afterwards, kill the server.
This is possible, but takes a lot of work to do correctly.
Running a process in the background
By running Specsheet with the
--exec command-line option set, it will spawn a process using the given shell command.
Waiting before running the first test
Servers can sometimes take a while to start up before being able to process incoming requests. If Specsheet sends requests too early, the first few tests will fail.
A shell script could just add a call to
sleep 5; after spawning the server in the background, delaying the time Specsheet gets started by five seconds. Specsheet offers this too, with the
--exec-delay option, which takes a value in seconds.
specsheet tests.toml --exec ./run_server.sh --exec-delay 3
However, there's another way: as Specsheet has access to the background process's output, it can wait until a line of output matches a pattern that signifies the server is running.
For example, say you want to test a server that prints
Server running (port 8080) when it's ready:
$ specsheet tests.toml --exec ./run_server.sh --exec-glob "Server running (port *)"
Using a delay and a glob pattern together will add the delay after a line matches the pattern.
If the process outputs writes to standard output or standard error, Specsheet will intersperse the lines of output with the times tests were started and stopped.