In this post, I’ll tell you how to create Puppet module test cases. If you are newbie with Puppet, I recommend you begin by my another post Hello Puppet!.

My using operating system is Xubuntu 14.04 LTS.

Hello Git!

As every project begin in Hello world, by the same way our project begin in Hello Git!

Create directory structure:

$ mkdir -p puppet/modules/git/manifests
$ cd puppet

Create init.pp in manifests:

$ nano modules/git/manifests/init.pp

And write bottom lines in file:

class git {

  package { 'git':
    ensure => 'installed',


Run our git module:

$ puppet apply --modulepath modules/ -e 'class {"git":}'

And now git should be installed to our system.

Create test case

Is time to go on and let make our first test case. In web, I have seen many different variations to make this part of post. But I like to make things easiest way as possible, so I’ll show you how to make this part as simplest I know so far.

Goto git module root:

$ cd modules/git

Write necessary gems in Gemfile.

$ nano Gemfile

source ''
puppetversion = ENV.key?('PUPPET_VERSION') ? "= #{ENV['PUPPET_VERSION']}" : ['>= 2.7']
gem 'puppet', puppetversion
gem 'puppetlabs_spec_helper', '>= 0.1.0'
gem 'rspec', '>=3.1.0'
gem 'rspec-puppet'

And bundle install gems:

$ bundle install

Run command rspec-puppet-init, and we got full nicely generated spec directory structure:

$ rspec-puppet-init

 + spec/
 + spec/classes/
 + spec/defines/
 + spec/functions/
 + spec/hosts/
 + spec/fixtures/
 + spec/fixtures/manifests/
 + spec/fixtures/modules/
 + spec/fixtures/modules/git/
 + spec/fixtures/manifests/site.pp
 + spec/fixtures/modules/git/manifests
 + spec/spec_helper.rb
 + Rakefile

Rspec-puppet for some reason miss require puppetlabs_spec_helper. We add it into bottom of file:

$ echo "require 'puppetlabs_spec_helper/rake_tasks'" >> Rakefile

Write some test case. That is the way, how we verify and be sure that our git class contain package git.

$ nano spec/classes/git_spec.rb

require 'spec_helper'

describe 'git', :type => 'class' do
  context 'install git' do
    it { should contain_package('git') }

And now we’re able to run our test case. Because we are using Gemfile, is very important to run rake spec with bundle! If you’re not using bundle, you’re not even use Gemfile, so you’re not using your specified gems versions! It isn’t good practice, so use bundle:

$ bundle exec rake spec

/home/niko/.rvm/rubies/ruby-2.1.2/bin/ruby -I/home/niko/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.1.7/lib:/home/niko/.rvm/gems/ruby-2.1.2/gems/rspec-support-3.1.2/lib/home/niko/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.1.7/exe/rspec --pattern spec/\{classes,defines,unit,functions,hosts,integration\}/\*\*/\*_spec.rb --color

Finished in 1.06 seconds (files took 0.78309 seconds to load)
1 example, 0 failures