Today I learned

Testing generated static sites

You can make a test script use grep and test to check for generated output. Since grep will die with an error code if it doesn't match anything, the test will be considered a failure.

test.sh
#!/usr/bin/env sh
set -o errexit # die on errors

grep "<title>" _site/index.html >/dev/null
grep display _site/style.css >/dev/null
grep function _site/script.js >/dev/null
test -f _site/image.jpg # check for file existence

Travis support

You can even integrate these with your Travis-CI tests. (Learn more about build configuration from their documentation.)

.travis.yml
before_script: bundle exec jekyll build
script: ./test/test.sh

Makefiles

Another good way is to use a Makefile so you can simply use make test to invoke tests.

Makefile
test: _site
    grep display _site/style.css >/dev/null
    grep function _site/script.js >/dev/null
    test -f _site/image.jpg #check for file existence

_site:
    bundle
    bundle exec jekyll build --safe

You have just read Testing generated static sites, written on May 29, 2015. This is Today I Learned, a collection of random tidbits I've learned through my day-to-day web development work. I'm Rico Sta. Cruz, @rstacruz on GitHub (and Twitter!).

← More articles