Coder Social home page Coder Social logo

shpec's Introduction

shpec Build Status Join the chat at https://gitter.im/rylnd/shpec

Test your shell scripts!

Screenshot of shpec

Using shpec

This repo itself is using shpec, so feel free to use it as an example. Here is the basic structure that you'll want:

└── shpec
    └── an_example_shpec.sh
    └── another_shpec.sh

Then to run your tests:

shpec [shpec_files]

If you'd like your tests to run automatically when they change, we recommend the entr utility:

find . -name "*_shpec.sh" | entr shpec

Note that there are some shell specificities, you'll find more about them in the Compatibility file.

Structuring your Tests

shpec is similar to other BDD frameworks like RSpec, Jasmine, and mocha.

The two main constructs are describe/end (used to group tests) and it/end (used to describe an individual test and wrap assertions).

Note: Since your test files will be sourced into shpec, you can use any shell command that would normally be available in your session.

Examples

shpec's own tests are a great place to start. For more examples, see the wiki page

Matchers

The general format of an assertion is:

assert matcher arguments

where matcher is one of the following:

Binary Matchers

equal         # equality
unequal       # inequality
gt            # algebraic '>'
lt            # algebraic '<'
match         # regex match
no_match      # lack of regex match

Unary Matchers

present       # string presence
blank         # string absence
file_present  # file presence
file_absent   # file absence
symlink       # tests a symlink's target
test          # evaluates a test string

Custom Matchers

Custom matchers are loaded from shpec/matchers/*.sh.

For example, here's how you'd create a still_alive matcher:

# in shpec/matchers/network.sh
still_alive() {
  ping -oc1 "$1" > /dev/null 2>&1
  assert equal "$?" 0
}

Then you can use that matcher like any other:

# in shpec/network_shpec.sh
describe "my server"
  it "serves responses"
    assert still_alive "my-site.com"
  end
end

Stubbing

You can stub commands using stub_command. This function takes the name of the command you wish to stub. If provided, the second argument will be used as the body of the command. (code that would be evaluated) Once you're done, you can delete it with unstub_command.

The best example is the shpec test for this feature.

Installation

you can either install with curl

sh -c "`curl -L https://raw.github.com/rylnd/shpec/master/install.sh`"

or with antigen for zsh by putting antigen bundle rylnd/shpec in your .zshrc

Contributing

Pull requests are always welcome.

If you've got a test or custom matcher you're particularly proud of, please consider adding it to the Examples page!

Style and code conventions

Language: POSIX shell

The core shpec script and function should work the same in any POSIX compliant shell. You can use shpec to test scripts that use non-POSIX features, but you must avoid them when extending shpec or the main shpec_shpech.sh tests.

Namespace

Any variables starting with _shpec_ are reserved for internal use and should not be used in test cases (except perhaps for test cases of shpec itself).

shpec's People

Contributors

rylnd avatar hlangeveld avatar adrieankhisbe avatar twe4ked avatar begriffs avatar gitter-badger avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.