john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

rpsls pytest advanced test

import py.test as pytest
import rpsls

@pytest.mark.parametrize(("name", "number"), [
    ("rock", 0),
    ("Spock", 1),
    ("paper", 2),
    ("lizard", 3),
    ("scissors", 4)
])
def test_name_to_number(name, number):
    ''' py.test uses metaprogramming to inject name and number params from the list above '''
    assert rpsls.name_to_number(name) == number

@pytest.mark.parametrize(("player1", "player2", "winner"), [
    (0, 0, 0),
    (0, 1, 2),
    (0, 2, 2),
    (0, 3, 1),
    (0, 4, 1),
    (1, 0, 1),
    (1, 1, 0),
    (1, 2, 2),
    (1, 3, 2),
    (1, 4, 1),
    (2, 0, 1),
    (2, 1, 1),
    (2, 2, 0),
    (2, 3, 2),
    (2, 4, 2),
    (3, 0, 2),
    (3, 1, 1),
    (3, 2, 1),
    (3, 3, 0),
    (3, 4, 2),
    (4, 0, 2),
    (4, 1, 2),
    (4, 2, 1),
    (4, 3, 1),
    (4, 4, 0)
])
def test_find_winner(player1, player2, winner):
    ''' py.test uses metaprogramming to inject player1 and player2 and winner params from the list above '''
    assert rpsls.find_winner(player1, player2) == winner

def test_find_winner_expect_error():
    ''' find_winner() only works on numbers so it should throw a TypeError if you pass strings to it '''
    pytest.raises(TypeError, rpsls.find_winner, "abc", "def")

@pytest.mark.skipif("True")
def test_find_winner_skip_me_always():
    ''' "True" is dynamically eval-ed so this test case is always skipped '''
    assert 1 == 2

import time
@pytest.mark.skipif("time.localtime()[6] == 6")
def test_find_winner_skip_me_on_sundays():
    ''' time.localtime()[6] returns a number representing the day of the week from 0 (Monday) to 6 (Sunday) '''
    assert time.localtime()[6] < 6

# my_slow_test_marker is a custom test case annotation that is interpreted from conftest.py
# and when you run py.test like this:
#      py.test --skipwslow
# it will skip this test using the logic in conftest.py
@pytest.mark.my_slow_test_marker
def test_find_winner_slow():
    ''' (please pretend that I am a slow test) '''
    assert rpsls.find_winner(-1, -2) == 1

  • « rpsls pytest simple test
  • rpsls »

Published

Jan 25, 2013

Category

python

~250 words

Tags

  • advanced 5
  • pytest 6
  • python 180
  • rpsls 6
  • test 29