Skip to content

Instantly share code, notes, and snippets.

@notro
Created June 26, 2018 20:59
Show Gist options
  • Save notro/8bda21a325044e0a41f0beeb7669128e to your computer and use it in GitHub Desktop.
Save notro/8bda21a325044e0a41f0beeb7669128e to your computer and use it in GitHub Desktop.
import pytest
import sys
sys.path.append('/home/pi')
import cpboard
test_obj_data = [
None,
True, False,
5,
4.2,
b'', b"Hello",
"", "\n", "Hello world",
"""Hello
world""",
(), (1,2),
[], [1,2],
{}, {'a' : 1, 'b' : 2},
set(), set([1,2]),
frozenset(), frozenset([1,2]),
]
@cpboard.remote
def board_test_obj(obj):
return obj
@pytest.mark.parametrize("obj", test_obj_data)
def test_obj(board, obj):
import sys
res = board_test_obj(board, obj, _out=sys.stdout)
#res = board_test_obj(board, obj)
assert obj == res
@cpboard.remote
def board_test_namedtuple():
import collections
Point = collections.namedtuple('Point', ['x', 'y'])
return Point(11, y=22)
def test_namedtuple(board):
import collections
Point = collections.namedtuple('Point', ['x', 'y'])
obj = Point(11, y=22)
res = board_test_namedtuple(board)
assert obj._asdict() == res._asdict()
assert type(obj).__name__ == type(res).__name__
test_args_data = [
( ( ), { } ),
( (1, ), { } ),
( (1, 2, ), { } ),
( ( ), { 'a' : 3 } ),
( ( ), { 'a' : 3, 'b' : 4 } ),
( (5, ), { 'a' : 3 } ),
( (5, 6, ), { 'a' : 3, 'b' : 4 } ),
( ('Hello',), { } ),
( ('Hello', ), { 'a' : 'world'} ),
( ( ), { 'a' : 'world'} ),
]
@cpboard.remote
def board_test_args(*args, **kwargs):
return (args, kwargs)
@pytest.mark.parametrize("args, kwargs", test_args_data)
def test_args(board, args, kwargs):
obj = (args, kwargs)
res = board_test_args(board, *args, **kwargs)
assert obj == res
@cpboard.remote
def board_test_exception_missing_argument(arg):
pass
@cpboard.remote
def board_test_exception_nameerror():
not_defined
@cpboard.remote
def board_test_exception_oserror():
raise OSError(110)
test_exception_data = [
(board_test_exception_missing_argument, TypeError, 'function takes 1 positional arguments but 0 were given'),
(board_test_exception_nameerror, NameError, 'not_defined'),
(board_test_exception_oserror, OSError, '110'),
]
@pytest.mark.parametrize("func, exc, val", test_exception_data)
def test_exception(board, func, exc, val):
with pytest.raises(exc) as excinfo:
func(board)
assert val in str(excinfo.value)
@cpboard.remote
def board_test_struct_time(tup):
import time
return time.struct_time(tup)
def test_struct_time(board):
tup = (2000, 1, 1, 0, 0, 0, 0, 0, 0)
import time
obj = time.struct_time(tup)
res = board_test_struct_time(board, tup)
assert obj == res
def test_os_uname(board):
import os
obj = os.uname()
res = cpboard.os_uname(board)
assert type(obj) == type(res)
@cpboard.remote
def board_test_print(obj):
print(obj.upper())
def test_print(board):
import io
out = io.StringIO()
obj = 'Hello world'
board_test_print(board, obj, _out=out)
res = out.getvalue()
expected = obj.upper()
assert expected in res
@cpboard.remote
def board_test_assert():
x = 5
y = 6
assert x == y, "%r vs (expected) %r" % (x, y)
#@pytest.mark.skip(reason="try without")
def test_assert(board):
board_test_assert(board)
@cpboard.pytest_wrap
def test_remote():
x = 11
y = 6
assert x == y, "%r vs (expected) %r" % (x, y)
# test unicode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment