Skip to content

Commit

Permalink
Merge pull request #3 from ros2/pytest_instead_of_nose
Browse files Browse the repository at this point in the history
use pytest instead of nose
  • Loading branch information
dirk-thomas committed Jun 25, 2018
2 parents 0fa5348 + 2d04bdc commit bf3a8e1
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 64 deletions.
1 change: 1 addition & 0 deletions rosidl_generator_py/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<test_depend>ament_index_python</test_depend>
<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>
<test_depend>pytest</test_depend>
<test_depend>python_cmake_module</test_depend>
<test_depend>rmw</test_depend>
<test_depend>rmw_implementation</test_depend>
Expand Down
141 changes: 77 additions & 64 deletions rosidl_generator_py/test/test_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from nose.tools import assert_raises
import pytest

from rosidl_generator_py.msg import Constants
from rosidl_generator_py.msg import Nested
Expand All @@ -37,37 +37,32 @@ def test_arrays_of_bounded_strings():
a.ub_string_static_array_value = array_valid_string_length
assert array_valid_string_length == a.ub_string_static_array_value

assert_raises(
AssertionError, setattr, a,
'ub_string_static_array_value', array_too_long_strings)
with pytest.raises(AssertionError):
setattr(a, 'ub_string_static_array_value', array_too_long_strings)

assert_raises(
AssertionError, setattr, a,
'ub_string_static_array_value', ['a' * 2, 'b' * 3])
with pytest.raises(AssertionError):
setattr(a, 'ub_string_static_array_value', ['a' * 2, 'b' * 3])

assert [] == a.ub_string_ub_array_value
a.ub_string_ub_array_value = array_valid_string_length
assert array_valid_string_length == a.ub_string_ub_array_value

assert_raises(
AssertionError, setattr, a,
'ub_string_ub_array_value', array_too_long_strings)
with pytest.raises(AssertionError):
setattr(a, 'ub_string_ub_array_value', array_too_long_strings)

array10strings = [] + [str(i) for i in range(10)]
a.ub_string_ub_array_value = array10strings
assert array10strings == a.ub_string_ub_array_value

assert_raises(
AssertionError, setattr, a,
'ub_string_ub_array_value', array10strings + ['gfg'])
with pytest.raises(AssertionError):
setattr(a, 'ub_string_ub_array_value', array10strings + ['gfg'])

assert [] == a.ub_string_dynamic_array_value
a.ub_string_dynamic_array_value = array_valid_string_length
assert array_valid_string_length == a.ub_string_dynamic_array_value

assert_raises(
AssertionError, setattr, a,
'ub_string_dynamic_array_value', array_too_long_strings)
with pytest.raises(AssertionError):
setattr(a, 'ub_string_dynamic_array_value', array_too_long_strings)

array10strings = [] + [str(i) for i in range(10)]
a.ub_string_dynamic_array_value = array10strings
Expand All @@ -80,17 +75,20 @@ def test_arrays_of_bounded_strings():
def test_invalid_attribute():
a = Strings()

assert_raises(AttributeError, setattr, a, 'invalid_string1', 'foo')
with pytest.raises(AttributeError):
setattr(a, 'invalid_string1', 'foo')

assert_raises(AttributeError, getattr, a, 'invalid_string2')
with pytest.raises(AttributeError):
getattr(a, 'invalid_string2')


def test_constructor():
a = Strings(empty_string='foo')

assert'foo' == a.empty_string

assert_raises(AssertionError, Strings, unknown_field='test')
with pytest.raises(AssertionError):
Strings(unknown_field='test')


def test_constants():
Expand All @@ -100,7 +98,8 @@ def test_constants():
assert '\x7F' == Constants.TOTO
assert b'0' == Constants.TATA

assert_raises(AttributeError, setattr, Constants, 'FOO', 'bar')
with pytest.raises(AttributeError):
setattr(Constants, 'FOO', 'bar')


def test_default_values():
Expand All @@ -117,7 +116,8 @@ def test_default_values():
assert 'Hello"world!' == a.DEF_STRING3__DEFAULT
assert 'Hello\'world!' == a.DEF_STRING4__DEFAULT
assert 'Hello"world!' == a.DEF_STRING5__DEFAULT
assert_raises(AttributeError, setattr, Strings, 'DEF_STRING__DEFAULT', 'bar')
with pytest.raises(AttributeError):
setattr(Strings, 'DEF_STRING__DEFAULT', 'bar')

b = StringArrays()
assert ['What', 'a', 'wonderful', 'world', '!'] == b.DEF_STRING_DYNAMIC_ARRAY_VALUE__DEFAULT
Expand All @@ -142,16 +142,19 @@ def test_check_constraints():
a = Strings()
a.empty_string = 'test'
assert 'test' == a.empty_string
assert_raises(AssertionError, setattr, a, 'empty_string', 1234)
with pytest.raises(AssertionError):
setattr(a, 'empty_string', 1234)
a.ub_string = 'a' * 22
assert 'a' * 22 == a.ub_string
assert_raises(AssertionError, setattr, a, 'ub_string', 'a' * 23)
with pytest.raises(AssertionError):
setattr(a, 'ub_string', 'a' * 23)

b = Nested()
primitives = Primitives()
b.primitives = primitives
assert b.primitives == primitives
assert_raises(AssertionError, setattr, b, 'primitives', 'foo')
with pytest.raises(AssertionError):
setattr(b, 'primitives', 'foo')

list_of_primitives = [primitives, primitives]
tuple_of_primitives = (primitives, primitives)
Expand All @@ -161,10 +164,12 @@ def test_check_constraints():
b.two_primitives = tuple_of_primitives
assert b.two_primitives == tuple_of_primitives
assert type(b.two_primitives) == tuple
assert_raises(AssertionError, setattr, b, 'two_primitives', Primitives())
assert_raises(AssertionError, setattr, b, 'two_primitives', [Primitives()])
assert_raises(AssertionError, setattr, b, 'two_primitives',
[primitives, primitives, primitives])
with pytest.raises(AssertionError):
setattr(b, 'two_primitives', Primitives())
with pytest.raises(AssertionError):
setattr(b, 'two_primitives', [Primitives()])
with pytest.raises(AssertionError):
setattr(b, 'two_primitives', [primitives, primitives, primitives])

b.up_to_three_primitives = []
assert [] == b.up_to_three_primitives
Expand All @@ -174,8 +179,10 @@ def test_check_constraints():
assert [primitives, primitives] == b.up_to_three_primitives
b.up_to_three_primitives = [primitives, primitives, primitives]
assert [primitives, primitives, primitives] == b.up_to_three_primitives
assert_raises(AssertionError, setattr, b, 'up_to_three_primitives',
[primitives, primitives, primitives, primitives])
with pytest.raises(AssertionError):
setattr(
b, 'up_to_three_primitives',
[primitives, primitives, primitives, primitives])

b.unbounded_primitives = [primitives, primitives]
assert [primitives, primitives] == b.unbounded_primitives
Expand All @@ -184,16 +191,22 @@ def test_check_constraints():
c.byte_value = b'a'
assert b'a' == c.byte_value
assert 'a' != c.byte_value
assert_raises(AssertionError, setattr, c, 'byte_value', 'a')
assert_raises(AssertionError, setattr, c, 'byte_value', b'abc')
assert_raises(AssertionError, setattr, c, 'byte_value', 'abc')
with pytest.raises(AssertionError):
setattr(c, 'byte_value', 'a')
with pytest.raises(AssertionError):
setattr(c, 'byte_value', b'abc')
with pytest.raises(AssertionError):
setattr(c, 'byte_value', 'abc')

c.char_value = 'a'
assert 'a' == c.char_value
assert b'a' != c.char_value
assert_raises(AssertionError, setattr, c, 'char_value', b'a')
assert_raises(AssertionError, setattr, c, 'char_value', 'abc')
assert_raises(AssertionError, setattr, c, 'char_value', b'abc')
with pytest.raises(AssertionError):
setattr(c, 'char_value', b'a')
with pytest.raises(AssertionError):
setattr(c, 'char_value', 'abc')
with pytest.raises(AssertionError):
setattr(c, 'char_value', b'abc')

c.up_to_three_int32_values = []
assert [] == c.up_to_three_int32_values
Expand All @@ -202,45 +215,45 @@ def test_check_constraints():
c.up_to_three_int32_values = [12345, -12345, 6789]
assert [12345, -12345, 6789] == c.up_to_three_int32_values
c.up_to_three_int32_values = [12345, -12345, 6789]
assert_raises(
AssertionError, setattr, c, 'up_to_three_int32_values', [12345, -12345, 6789, -6789])
with pytest.raises(AssertionError):
setattr(c, 'up_to_three_int32_values', [12345, -12345, 6789, -6789])

c.up_to_three_string_values = []
assert [] == c.up_to_three_string_values
c.up_to_three_string_values = ['foo', 'bar']
assert ['foo', 'bar'] == c.up_to_three_string_values
c.up_to_three_string_values = ['foo', 'bar', 'baz']
assert ['foo', 'bar', 'baz'] == c.up_to_three_string_values
assert_raises(
AssertionError, setattr, c, 'up_to_three_string_values', ['foo', 'bar', 'baz', 'hello'])
with pytest.raises(AssertionError):
setattr(c, 'up_to_three_string_values', ['foo', 'bar', 'baz', 'hello'])


def test_out_of_range():
a = Primitives()
assert_raises(
AssertionError, setattr, a, 'char_value', '\x80')
with pytest.raises(AssertionError):
setattr(a, 'char_value', '\x80')
for i in [8, 16, 32, 64]:
assert_raises(
AssertionError, setattr, a, 'int%d_value' % i, 2**(i - 1))
assert_raises(
AssertionError, setattr, a, 'int%d_value' % i, -2**(i - 1) - 1)
assert_raises(
AssertionError, setattr, a, 'uint%d_value' % i, -1)
assert_raises(
AssertionError, setattr, a, 'int%d_value' % i, 2**i)
with pytest.raises(AssertionError):
setattr(a, 'int%d_value' % i, 2**(i - 1))
with pytest.raises(AssertionError):
setattr(a, 'int%d_value' % i, -2**(i - 1) - 1)
with pytest.raises(AssertionError):
setattr(a, 'uint%d_value' % i, -1)
with pytest.raises(AssertionError):
setattr(a, 'int%d_value' % i, 2**i)

b = Various()
assert_raises(
AssertionError, setattr, b, 'two_uint16_value', [2**16])
assert_raises(
AssertionError, setattr, b, 'two_uint16_value', [-1])

assert_raises(
AssertionError, setattr, b, 'up_to_three_int32_values', [2**31])
assert_raises(
AssertionError, setattr, b, 'up_to_three_int32_values', [-2**31 - 1])

assert_raises(
AssertionError, setattr, b, 'unbounded_uint64_values', [2**64])
assert_raises(
AssertionError, setattr, b, 'unbounded_uint64_values', [-1])
with pytest.raises(AssertionError):
setattr(b, 'two_uint16_value', [2**16])
with pytest.raises(AssertionError):
setattr(b, 'two_uint16_value', [-1])

with pytest.raises(AssertionError):
setattr(b, 'up_to_three_int32_values', [2**31])
with pytest.raises(AssertionError):
setattr(b, 'up_to_three_int32_values', [-2**31 - 1])

with pytest.raises(AssertionError):
setattr(b, 'unbounded_uint64_values', [2**64])
with pytest.raises(AssertionError):
setattr(b, 'unbounded_uint64_values', [-1])

0 comments on commit bf3a8e1

Please sign in to comment.