From 2d04bdc9fcafbbb442c7cdaff52773da02695ca7 Mon Sep 17 00:00:00 2001 From: Dirk Thomas Date: Mon, 25 Jun 2018 14:07:28 -0700 Subject: [PATCH] use pytest instead of nose --- rosidl_generator_py/package.xml | 1 + rosidl_generator_py/test/test_interfaces.py | 141 +++++++++++--------- 2 files changed, 78 insertions(+), 64 deletions(-) diff --git a/rosidl_generator_py/package.xml b/rosidl_generator_py/package.xml index e1600fe0..00fe6a5d 100644 --- a/rosidl_generator_py/package.xml +++ b/rosidl_generator_py/package.xml @@ -28,6 +28,7 @@ ament_index_python ament_lint_auto ament_lint_common + pytest python_cmake_module rmw rmw_implementation diff --git a/rosidl_generator_py/test/test_interfaces.py b/rosidl_generator_py/test/test_interfaces.py index 112a1987..e9f59c2d 100644 --- a/rosidl_generator_py/test/test_interfaces.py +++ b/rosidl_generator_py/test/test_interfaces.py @@ -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 @@ -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 @@ -80,9 +75,11 @@ 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(): @@ -90,7 +87,8 @@ def test_constructor(): assert'foo' == a.empty_string - assert_raises(AssertionError, Strings, unknown_field='test') + with pytest.raises(AssertionError): + Strings(unknown_field='test') def test_constants(): @@ -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(): @@ -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 @@ -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) @@ -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 @@ -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 @@ -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 @@ -202,8 +215,8 @@ 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 @@ -211,36 +224,36 @@ def test_check_constraints(): 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])