1up4developers logo

1up4developers

Nadando contra o Waterfall. tail -f /mind/realworld >> /blog

[QuickTips] Usando Shell Script Como Suite De Teste

| | Comments


Indo na linha do programador pragmático, às vezes, dependendo do time ou linguagem do projeto, é muito mais prático usar shell script para testar. Recentemente resolvi usar esta abordagem, e cheguei no seguinte “template” em shell script para testar uma API Server.

Dependências:

  • bash

  • curl

Foi testado no Ubuntu 13.04.

Obs: Vou embedar este gist, e pode ser que não apareça no seu reader ;D.

Obs2: Este post é uma versão pt-br deste post no coderwall Using Shell Script to test your server.

test.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  #!/bin/bash

URL=http://localhost:8080

## Unit-Testable Shell Scripts (http://eradman.com/posts/ut-shell-scripts.html)
typeset -i tests_run=0
function try { this="$1"; }
trap 'printf "$0: exit code $? on line $LINENO\nFAIL: $this\n"; exit 1' ERR
function assert {
    let tests_run+=1
    [ "$1" = "$2" ] && { echo -n "."; return; }
    printf "\nFAIL: $this\n'$1' != '$2'\n"; exit 1
}
## end

###############################################################

try "Example of GET and test for 404 status"

out=$(curl -s -w "%{http_code}" $URL)
assert "404" "$out"

try "Example of POST XML"

# Post xml (from hello.xml file) on /hello
out=$(cat test/hello.xml | curl -s -H "Content-Type: text/xml" -d @- \
  -X POST $URL/hello)
assert "Hello World" "$out"

###############################################################
echo
echo "PASS: $tests_run tests run"

Referências:

Unit-Testable Shell Scripts

Aguardo dicas, sugestões, experiências etc.

Comments