From f0f3cf6f65028b6b776c1eead4c0bac3231a18e6 Mon Sep 17 00:00:00 2001 From: Gretel Date: Wed, 17 Jan 2024 14:23:23 -0500 Subject: [PATCH] replace TestCase with pytest --- setup.py | 1 + .../test_inheritance.py | 46 ++++----- tests/unit/test_foundation/test_base.py | 76 +++++++------- .../unit/test_foundation/test_basic_types.py | 93 ++++++++--------- .../test_foundation/test_network_types.py | 79 +++++++-------- tests/unit/test_struct.py | 11 ++- tests/unit/test_utils.py | 13 +-- .../v0x01/test_asynchronous/test_error_msg.py | 4 +- .../unit/v0x01/test_common/test_flow_match.py | 13 +-- tests/unit/v0x01/test_common/test_header.py | 21 ++-- tests/unit/v0x01/test_common/test_phy_port.py | 22 ++--- tests/unit/v0x01/test_common/test_queue.py | 31 +++--- .../test_controller2switch/test_packet_out.py | 17 ++-- .../v0x04/test_asynchronous/test_packet_in.py | 7 +- .../unit/v0x04/test_common/test_flow_match.py | 29 +++--- .../test_controller2switch/test_group_mod.py | 99 +++++++++---------- .../test_controller2switch/test_meter_mod.py | 22 ++--- .../test_controller2switch/test_port_mod.py | 4 +- .../test_controller2switch/test_table_mod.py | 6 +- tests/unit/v0x04/test_struct.py | 9 +- 20 files changed, 303 insertions(+), 300 deletions(-) diff --git a/setup.py b/setup.py index 1bd713a3b..c6fc48338 100644 --- a/setup.py +++ b/setup.py @@ -8,6 +8,7 @@ # Disabling checks due to https://github.com/PyCQA/pylint/issues/73 # pylint: disable=import-error,no-name-in-module,unspecified-encoding # pylint: disable=consider-using-with +# pylint: disable=deprecated-module from distutils.command.clean import clean # pylint: enable=import-error,no-name-in-module from subprocess import CalledProcessError, call, check_call diff --git a/tests/unit/test_class_inheritance/test_inheritance.py b/tests/unit/test_class_inheritance/test_inheritance.py index 025962814..b008a85c6 100644 --- a/tests/unit/test_class_inheritance/test_inheritance.py +++ b/tests/unit/test_class_inheritance/test_inheritance.py @@ -5,10 +5,10 @@ from pyof.foundation.basic_types import UBInt8, UBInt16, UBInt32, UBInt64 -class TestInheritance(unittest.TestCase): +class TestInheritance: """Testing GenericStruct class inheritance.""" - def setUp(self): + def setup_method(self): """Basic Test Setup.""" class MyClassA(GenericStruct): """Example class.""" @@ -41,30 +41,30 @@ def test_modifications(self): m1 = self.MyClassA() m2 = self.MyClassB() # Checking keys (attributes names) and its ordering - self.assertEqual([attr[0] for attr in m1.get_class_attributes()], - self.a_expected_names) - self.assertEqual([attr[0] for attr in m2.get_class_attributes()], - self.b_expected_names) + assert [attr[0] for attr in m1.get_class_attributes()] == \ + self.a_expected_names + assert [attr[0] for attr in m2.get_class_attributes()] == \ + self.b_expected_names # Check if there is no shared attribute between instances - self.assertIsNot(m1, m2) - self.assertIsNot(m1.a1, m2.a1) - self.assertIsNot(m1.a2, m2.a2) - self.assertIsNot(m1.a3, m2.a4) - self.assertIsNot(m1.a4, m2.a4) - self.assertIsNot(m1.a5, m2.a5) + assert m1 is not m2 + assert m1.a1 is not m2.a1 + assert m1.a2 is not m2.a2 + assert m1.a3 is not m2.a4 + assert m1.a4 is not m2.a4 + assert m1.a5 is not m2.a5 # Check attributes types on MyClassA - self.assertIsInstance(self.MyClassA.a1, UBInt8) - self.assertIsInstance(self.MyClassA.a2, UBInt16) - self.assertIsInstance(self.MyClassA.a3, UBInt8) - self.assertIsInstance(self.MyClassA.a4, UBInt16) - self.assertIsInstance(self.MyClassA.a5, UBInt32) + assert isinstance(self.MyClassA.a1, UBInt8) + assert isinstance(self.MyClassA.a2, UBInt16) + assert isinstance(self.MyClassA.a3, UBInt8) + assert isinstance(self.MyClassA.a4, UBInt16) + assert isinstance(self.MyClassA.a5, UBInt32) # Check attributes types on MyClassA - self.assertIsInstance(self.MyClassB.a1, UBInt8) - self.assertIsInstance(self.MyClassB.a2, UBInt64) - self.assertIsInstance(self.MyClassB.a4, UBInt16) - self.assertIsInstance(self.MyClassB.a5, UBInt32) - self.assertIsInstance(self.MyClassB.a0, UBInt32) - self.assertIsInstance(self.MyClassB.b6, UBInt8) + assert isinstance(self.MyClassB.a1, UBInt8) + assert isinstance(self.MyClassB.a2, UBInt64) + assert isinstance(self.MyClassB.a4, UBInt16) + assert isinstance(self.MyClassB.a5, UBInt32) + assert isinstance(self.MyClassB.a0, UBInt32) + assert isinstance(self.MyClassB.b6, UBInt8) diff --git a/tests/unit/test_foundation/test_base.py b/tests/unit/test_foundation/test_base.py index 3d18f3daf..bb31638d4 100644 --- a/tests/unit/test_foundation/test_base.py +++ b/tests/unit/test_foundation/test_base.py @@ -4,10 +4,10 @@ from pyof.foundation import base, basic_types -class TestGenericStruct(unittest.TestCase): +class TestGenericStruct: """Testing GenericStruct class.""" - def setUp(self): + def setup_method(self): """Basic Test Setup.""" class AttributeA(base.GenericStruct): """Example class.""" @@ -52,18 +52,18 @@ def test_basic_attributes(self): """[Foundation/Base/GenericStruct] - Attributes Creation.""" message1 = self.MyMessage() message2 = self.MyMessage() - self.assertIsNot(message1, message2) - self.assertIsNot(message1.i, message2.i) - self.assertIsNot(message1.a, message2.a) - self.assertIsNot(message1.b, message2.b) - self.assertIsNot(message1.a.a1, message2.a.a1) - self.assertIsNot(message1.a.a2, message2.a.a2) - self.assertIsNot(message1.b.c, message2.b.c) - self.assertIsNot(message1.b.c.c1, message2.b.c.c1) - self.assertIsNot(message1.b.c.c2, message2.b.c.c2) - - -class TestGenericType(unittest.TestCase): + assert message1 is not message2 + assert message1.i is not message2.i + assert message1.a is not message2.a + assert message1.b is not message2.b + assert message1.a.a1 is not message2.a.a1 + assert message1.a.a2 is not message2.a.a2 + assert message1.b.c is not message2.b.c + assert message1.b.c.c1 is not message2.b.c.c1 + assert message1.b.c.c2 is not message2.b.c.c2 + + +class TestGenericType: """Testing GenericType class.""" def test_basic_operator(self): @@ -71,27 +71,27 @@ def test_basic_operator(self): a = basic_types.UBInt32(1) b = basic_types.UBInt32(2) - self.assertEqual(a + 1, 2) - self.assertEqual(1 + a, 2) - self.assertEqual(b + 1, 3) - self.assertEqual(1 + b, 3) - - self.assertEqual(a - 1, 0) - self.assertEqual(1 - a, 0) - self.assertEqual(b - 1, 1) - self.assertEqual(1 - b, 1) - - self.assertEqual(a & 1, 1) - self.assertEqual(1 & a, 1) - self.assertEqual(b & 1, 0) - self.assertEqual(1 & b, 0) - - self.assertEqual(a | 1, 1) - self.assertEqual(1 | a, 1) - self.assertEqual(b | 1, 3) - self.assertEqual(1 | b, 3) - - self.assertEqual(a ^ 1, 0) - self.assertEqual(1 ^ a, 0) - self.assertEqual(b ^ 1, 3) - self.assertEqual(1 ^ b, 3) + assert a + 1 == 2 + assert 1 + a == 2 + assert b + 1 == 3 + assert 1 + b == 3 + + assert a - 1 == 0 + assert 1 - a == 0 + assert b - 1 == 1 + assert 1 - b == 1 + + assert a & 1 == 1 + assert 1 & a == 1 + assert b & 1 == 0 + assert 1 & b == 0 + + assert a | 1 == 1 + assert 1 | a == 1 + assert b | 1 == 3 + assert 1 | b == 3 + + assert a ^ 1 == 0 + assert 1 ^ a == 0 + assert b ^ 1 == 3 + assert 1 ^ b == 3 diff --git a/tests/unit/test_foundation/test_basic_types.py b/tests/unit/test_foundation/test_basic_types.py index 59a6b9c14..28e4769e1 100644 --- a/tests/unit/test_foundation/test_basic_types.py +++ b/tests/unit/test_foundation/test_basic_types.py @@ -1,4 +1,5 @@ """Tests for Python-openflow BasicTypes.""" +import pytest import unittest from pyof.foundation import basic_types @@ -6,101 +7,101 @@ from pyof.foundation.basic_types import BinaryData -class TestUBInt8(unittest.TestCase): +class TestUBInt8: """Test of UBInt8 BasicType.""" - def setUp(self): + def setup_method(self): """Basic test setup.""" self.ubint8 = basic_types.UBInt8(255) def test_get_size(self): """[Foundation/BasicTypes/UBInt8] - size 1.""" - self.assertEqual(self.ubint8.get_size(), 1) + assert self.ubint8.get_size() == 1 def test_pack(self): """[Foundation/BasicTypes/UBInt8] - packing.""" - self.assertEqual(self.ubint8.pack(), b'\xff') + assert self.ubint8.pack() == b'\xff' def test_unpack(self): """[Foundation/BasicTypes/UBInt8] - unpacking.""" u = basic_types.UBInt8() u.unpack(b'\xfe') - self.assertEqual(u.value, 254) + assert u.value == 254 def test_pack_error(self): """[Foundation/BasicTypes/UBInt8] - packing exception.""" u = basic_types.UBInt8(256) - self.assertRaises(PackException, u.pack) + pytest.raises(PackException, u.pack) def test_cast_to_int(self): - self.assertEqual(255, int(self.ubint8)) + assert 255 == int(self.ubint8) -class TestUBInt16(unittest.TestCase): +class TestUBInt16: """Test of UBInt16 BasicType.""" - def setUp(self): + def setup_method(self): """Basic test setup.""" self.ubint16 = basic_types.UBInt16() def test_get_size(self): """[Foundation/BasicTypes/UBInt16] - size 2.""" - self.assertEqual(self.ubint16.get_size(), 2) + assert self.ubint16.get_size() == 2 -class TestUBInt32(unittest.TestCase): +class TestUBInt32: """Test of UBInt32 BasicType.""" - def setUp(self): + def setup_method(self): """Basic test setup.""" self.ubint32 = basic_types.UBInt32() def test_get_size(self): """[Foundation/BasicTypes/UBInt32] - size 4.""" - self.assertEqual(self.ubint32.get_size(), 4) + assert self.ubint32.get_size() == 4 -class TestUBInt64(unittest.TestCase): +class TestUBInt64: """Test of UBInt64 BasicType.""" - def setUp(self): + def setup_method(self): """Basic test setup.""" self.ubint64 = basic_types.UBInt64() def test_get_size(self): """[Foundation/BasicTypes/UBInt64] - size 8.""" - self.assertEqual(self.ubint64.get_size(), 8) + assert self.ubint64.get_size() == 8 -class TestUBInt128(unittest.TestCase): +class TestUBInt128: """Test of UBInt128 BasicType.""" - def setUp(self): + def setup_method(self): """Basic test setup.""" self.ubint128 = basic_types.UBInt128() def test_get_size(self): """[Foundation/BasicTypes/UBInt128] - size 16.""" - self.assertEqual(self.ubint128.get_size(), 16) + assert self.ubint128.get_size() == 16 -class TestChar(unittest.TestCase): +class TestChar: """Test of Char BasicType.""" - def setUp(self): + def setup_method(self): """Basic test setup.""" self.char1 = basic_types.Char('foo', length=3) self.char2 = basic_types.Char('foo', length=5) def test_get_size(self): """[Foundation/BasicTypes/Char] - get_size.""" - self.assertEqual(self.char1.get_size(), 3) - self.assertEqual(self.char2.get_size(), 5) + assert self.char1.get_size() == 3 + assert self.char2.get_size() == 5 def test_pack(self): """[Foundation/BasicTypes/Char] - packing.""" - self.assertEqual(self.char1.pack(), b'fo\x00') - self.assertEqual(self.char2.pack(), b'foo\x00\x00') + assert self.char1.pack() == b'fo\x00' + assert self.char2.pack() == b'foo\x00\x00' def test_unpack(self): """[Foundation/BasicTypes/Char] - unpacking.""" @@ -109,11 +110,11 @@ def test_unpack(self): char1.unpack(b'fo\x00') char2.unpack(b'foo\x00\x00') - self.assertEqual(char1.value, 'fo') - self.assertEqual(char2.value, 'foo') + assert char1.value == 'fo' + assert char2.value == 'foo' -class TestHWAddress(unittest.TestCase): +class TestHWAddress: """Test of HWAddress BasicType.""" def test_unpack_packed(self): @@ -123,7 +124,7 @@ def test_unpack_packed(self): packed = hw_addr.pack() unpacked = basic_types.HWAddress() unpacked.unpack(packed) - self.assertEqual(mac, unpacked.value) + assert mac == unpacked.value def test_default_value(self): """Testing default_value for HWAddress.""" @@ -132,10 +133,10 @@ def test_default_value(self): packed = hw_addr.pack() unpacked = basic_types.HWAddress() unpacked.unpack(packed) - self.assertEqual(mac, unpacked.value) + assert mac == unpacked.value -class TestIPAddress(unittest.TestCase): +class TestIPAddress: """Test of IPAddress BasicType.""" def test_unpack_packed(self): @@ -144,7 +145,7 @@ def test_unpack_packed(self): packed = ip_addr.pack() unpacked = basic_types.IPAddress() unpacked.unpack(packed) - self.assertEqual(ip_addr.value, unpacked.value) + assert ip_addr.value == unpacked.value def test_unpack_packed_with_netmask(self): """Testing unpack of packed IPAddress with netmask.""" @@ -152,65 +153,65 @@ def test_unpack_packed_with_netmask(self): packed = ip_addr.pack() unpacked = basic_types.IPAddress() unpacked.unpack(packed) - self.assertEqual(ip_addr.value, unpacked.value) + assert ip_addr.value == unpacked.value def test_netmask(self): """Testing get netmask from IPAddress.""" ip_addr = basic_types.IPAddress('192.168.0.1/24') - self.assertEqual(ip_addr.netmask, 24) + assert ip_addr.netmask == 24 ip_addr = basic_types.IPAddress('192.168.0.1/16') - self.assertEqual(ip_addr.netmask, 16) + assert ip_addr.netmask == 16 ip_addr = basic_types.IPAddress('192.168.0.1') - self.assertEqual(ip_addr.netmask, 32) + assert ip_addr.netmask == 32 def test_max_prefix(self): """Testing get max_prefix from IPAddress.""" ip_addr = basic_types.IPAddress() - self.assertEqual(ip_addr.max_prefix, 32) + assert ip_addr.max_prefix == 32 ip_addr = basic_types.IPAddress('192.168.0.35/16') - self.assertEqual(ip_addr.max_prefix, 32) + assert ip_addr.max_prefix == 32 def test_get_size(self): """Testing get_size from IPAddress.""" ip_addr = basic_types.IPAddress('192.168.0.1/24') - self.assertEqual(ip_addr.get_size(), 4) + assert ip_addr.get_size() == 4 -class TestBinaryData(unittest.TestCase): +class TestBinaryData: """Test Binary data type.""" def test_default_value(self): """Default packed value should be an empty byte.""" expected = b'' actual = BinaryData().pack() - self.assertEqual(expected, actual) + assert expected == actual def test_pack_none_value(self): """Test packing None value.""" expected = b'' actual = BinaryData(None).pack() - self.assertEqual(expected, actual) + assert expected == actual def test_pack_bytes_value(self): """Test packing some bytes.""" expected = b'forty two' actual = BinaryData(expected).pack() - self.assertEqual(expected, actual) + assert expected == actual def test_pack_empty_bytes(self): """Test packing empty bytes.""" expected = b'' actual = BinaryData(expected).pack() - self.assertEqual(expected, actual) + assert expected == actual def test_pack_packable_value(self): """Test packing packable value.""" hw_addr = basic_types.HWAddress('0a:d3:98:a5:30:47') expected = hw_addr.pack() actual = BinaryData(hw_addr).pack() - self.assertEqual(expected, actual) + assert expected == actual def test_unexpected_value_as_parameter(self): """Should raise ValueError if pack value is not bytes.""" data = BinaryData('Some string') - self.assertRaises(ValueError, data.pack, "can't be a string") + pytest.raises(ValueError, data.pack, "can't be a string") diff --git a/tests/unit/test_foundation/test_network_types.py b/tests/unit/test_foundation/test_network_types.py index 7cef9a939..2c421c64b 100644 --- a/tests/unit/test_foundation/test_network_types.py +++ b/tests/unit/test_foundation/test_network_types.py @@ -1,4 +1,5 @@ """Test Python-openflow network types.""" +import pytest import unittest from pyof.foundation.basic_types import BinaryData @@ -7,7 +8,7 @@ ARP, VLAN, Ethernet, GenericTLV, IPv4, IPv6) -class TestARP(unittest.TestCase): +class TestARP: """Test ARP packets, without Ethernet headers.""" def test_arp_pack(self): @@ -17,7 +18,7 @@ def test_arp_pack(self): packed = arp.pack() expected = b'\x00\x01\x08\x00\x06\x04\x00\x01\x00\x15\xaf\xd58\x98\xac' expected += b'\x10\x00\n\x00\x00\x00\x00\x00\x00\xac\x10\n\x14' - self.assertEqual(packed, expected) + assert packed == expected def test_arp_unpack(self): """Test unpack method of ARP class.""" @@ -27,14 +28,14 @@ def test_arp_unpack(self): tha='00:15:af:d5:38:98', tpa='172.16.0.10') unpacked = ARP() unpacked.unpack(raw) - self.assertEqual(unpacked, expected) + assert unpacked == expected def test_unpack_invalid_htype(self): """Raise UnpackException when L2 protocol is not Ethernet.""" raw = b'\x01\x23\x08\x00\x06\x04\x00\x02\x00\x1f:>\x9a\xcf\xac\x10\n' raw += b'\x14\x00\x15\xaf\xd58\x98\xac\x10\x00\n' arp = ARP() - with self.assertRaises(UnpackException): + with pytest.raises(UnpackException): arp.unpack(raw) def test_unpack_invalid_ptype(self): @@ -42,35 +43,35 @@ def test_unpack_invalid_ptype(self): raw = b'\x00\x01\x08\x90\x06\x04\x00\x02\x00\x1f:>\x9a\xcf\xac\x10\n' raw += b'\x14\x00\x15\xaf\xd58\x98\xac\x10\x00\n' arp = ARP() - with self.assertRaises(UnpackException): + with pytest.raises(UnpackException): arp.unpack(raw) -class TestNetworkTypes(unittest.TestCase): +class TestNetworkTypes: """Reproduce bugs found.""" - def test_GenTLV_value_unpack(self): + def test_gen_tlv_value_unpack(self): """Value attribute should be the same after unpacking.""" value = BinaryData(b'test') tlv = GenericTLV(value=value) tlv_unpacked = GenericTLV() tlv_unpacked.unpack(tlv.pack()) - self.assertEqual(tlv.value.value, tlv_unpacked.value.value) + assert tlv.value.value == tlv_unpacked.value.value -class TestEthernet(unittest.TestCase): +class TestEthernet: """Test Ethernet frames.""" - def test_Ethernet_pack(self): + def test_ethernet_pack(self): """Test pack method of Ethernet class without VLAN tag.""" ethernet = Ethernet(destination='00:1f:3a:3e:9a:cf', source='00:15:af:d5:38:98', ether_type=0x800, data=b'testdata') packed = ethernet.pack() expected = b'\x00\x1f:>\x9a\xcf\x00\x15\xaf\xd58\x98\x08\x00testdata' - self.assertEqual(packed, expected) + assert packed == expected - def test_Ethernet_unpack(self): + def test_ethernet_unpack(self): """Test pack method of Ethernet class without VLAN tag.""" raw = b'\x00\x15\xaf\xd58\x98\x00\x1f:>\x9a\xcf\x08\x00testdata' expected = Ethernet(destination='00:15:af:d5:38:98', @@ -79,9 +80,9 @@ def test_Ethernet_unpack(self): expected.pack() unpacked = Ethernet() unpacked.unpack(raw) - self.assertEqual(unpacked, expected) + assert unpacked == expected - def test_Tagged_Ethernet_pack(self): + def test_tagged_ethernet_pack(self): """Test pack method of Ethernet class including VLAN tag.""" ethernet = Ethernet(destination='00:1f:3a:3e:9a:cf', source='00:15:af:d5:38:98', vlans=[VLAN(vid=200)], @@ -89,9 +90,9 @@ def test_Tagged_Ethernet_pack(self): packed = ethernet.pack() expected = b'\x00\x1f:>\x9a\xcf\x00\x15\xaf\xd58' expected += b'\x98\x81\x00\x00\xc8\x08\x00testdata' - self.assertEqual(packed, expected) + assert packed == expected - def test_Tagged_Ethernet_unpack(self): + def test_tagged_ethernet_unpack(self): """Test pack method of Ethernet class including VLAN tag.""" raw = b'\x00\x15\xaf\xd58\x98\x00\x1f:>' raw += b'\x9a\xcf\x81\x00!^\x08\x00testdata' @@ -102,39 +103,39 @@ def test_Tagged_Ethernet_unpack(self): expected.pack() unpacked = Ethernet() unpacked.unpack(raw) - self.assertEqual(unpacked, expected) + assert unpacked == expected -class TestVLAN(unittest.TestCase): +class TestVLAN: """Test VLAN headers.""" - def test_VLAN_pack(self): + def test_vlan_pack(self): """Test pack method of VLAN class.""" vlan = VLAN(pcp=3, vid=20) packed = vlan.pack() expected = b'\x81\x00`\x14' - self.assertEqual(packed, expected) + assert packed == expected - def test_VLAN_unpack(self): + def test_vlan_unpack(self): """Test unpack method of VLAN class.""" raw = b'\x81\x00\xa0{' expected = VLAN(pcp=5, vid=123) unpacked = VLAN() unpacked.unpack(raw) - self.assertEqual(unpacked, expected) + assert unpacked == expected def test_unpack_wrong_tpid(self): """Raise UnpackException if the tpid is not VLAN_TPID.""" raw = b'\x12\x34\xa0{' vlan = VLAN() - with self.assertRaises(UnpackException): + with pytest.raises(UnpackException): vlan.unpack(raw) -class TestIPv4(unittest.TestCase): +class TestIPv4: """Test IPv4 packets.""" - def test_IPv4_pack(self): + def test_ipv4_pack(self): """Test pack/unpack of IPv4 class.""" packet = IPv4(dscp=10, ttl=64, protocol=17, source="192.168.0.10", destination="172.16.10.30", options=b'1000', @@ -142,9 +143,9 @@ def test_IPv4_pack(self): packed = packet.pack() expected = b'F(\x00 \x00\x00\x00\x00@\x11\x02' expected += b'\xc5\xc0\xa8\x00\n\xac\x10\n\x1e1000testdata' - self.assertEqual(packed, expected) + assert packed == expected - def test_IPv4_unpack(self): + def test_ipv4_unpack(self): """Test unpack of IPv4 binary packet.""" raw = b'FP\x00$\x00\x00\x00\x00\x80\x06W' raw += b'\xf4\n\x9aN\x81\xc0\xa8\xc7\xcc1000somemoredata' @@ -154,29 +155,29 @@ def test_IPv4_unpack(self): expected.pack() unpacked = IPv4() unpacked.unpack(raw) - self.assertEqual(unpacked, expected) + assert unpacked == expected - def test_IPv4_size(self): + def test_ipv4_size(self): """Test Header size for IPv4 packet.""" packet = IPv4() packet.pack() - self.assertEqual(20, packet.get_size()) - self.assertEqual(20, packet.length) - self.assertEqual(20, packet.ihl * 4) + assert 20 == packet.get_size() + assert 20 == packet.length + assert 20 == packet.ihl * 4 - def test_IPv4_checksum(self): + def test_ipv4_checksum(self): """Test if the IPv4 checksum is being calculated correclty.""" packet = IPv4(dscp=10, ttl=64, protocol=17, source="192.168.0.10", destination="172.16.10.30", options=b'1000', data=b'testdata') packet.pack() - self.assertEqual(packet.checksum, 709) + assert packet.checksum == 709 -class TestIPv6(unittest.TestCase): +class TestIPv6: """Test IPv6 packets.""" - def test_IPv6_pack(self): + def test_ipv6_pack(self): """Test pack/unpack of IPv6 class.""" packet = IPv6(next_header=6, hop_limit=64, source="::1", destination="::2", data=b'testdata') @@ -185,9 +186,9 @@ def test_IPv6_pack(self): expected += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' expected += b'\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' expected += b'\x00\x00\x00\x02testdata' - self.assertEqual(packed, expected) + assert packed == expected - def test_IPv6_unpack(self): + def test_ipv6_unpack(self): """Test unpack of IPv6 binary packet.""" raw = b'`\x00\x00\x00\x00\x0c\x06@\x00\x00' raw += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' @@ -198,4 +199,4 @@ def test_IPv6_unpack(self): expected.pack() unpacked = IPv6() unpacked.unpack(raw) - self.assertEqual(unpacked, expected) + assert unpacked == expected diff --git a/tests/unit/test_struct.py b/tests/unit/test_struct.py index e6918b157..3ccdb108b 100644 --- a/tests/unit/test_struct.py +++ b/tests/unit/test_struct.py @@ -1,11 +1,12 @@ """Automate struct tests.""" +import pytest import unittest from pyof.foundation.base import GenericMessage from tests.unit.raw_dump import RawDump -class TestStruct(unittest.TestCase): +class TestStruct: """Run tests related to struct packing and unpacking. Test the lib with raw dump files from an OpenFlow switch. We assume the @@ -147,7 +148,7 @@ def test_raw_dump_file(self): try: file_bytes = self.get_raw_dump().read() except FileNotFoundError: - raise self.skipTest('No raw dump file found.') + raise pytest.skip('No raw dump file found.') pyof_obj = self.get_raw_object() self._test_pack(pyof_obj, file_bytes) @@ -171,12 +172,12 @@ def _test_unpack(self, pyof_obj, bytes2unpack=None): bytes2unpack = bytes2unpack[8:unpacked.header.length.value] unpacked.unpack(bytes2unpack) - self.assertEqual(pyof_obj, unpacked) - self.assertEqual(pyof_obj.get_size(), unpacked.get_size()) + assert pyof_obj == unpacked + assert pyof_obj.get_size() == unpacked.get_size() def test_minimum_size(self): """Test struct minimum size.""" obj = TestStruct._msg_cls() if self._min_size is None: raise Exception(f'{self.__class__.__name__}._min_size is not set') - self.assertEqual(obj.get_size(), self._min_size) + assert obj.get_size() == self._min_size diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index af46fe1fd..90d16142b 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -1,4 +1,5 @@ """Automate utils tests.""" +import pytest import unittest from pyof.utils import UnpackException, unpack, validate_packet @@ -6,34 +7,34 @@ from pyof.v0x04.symmetric.hello import Hello as Hello_v0x04 -class TestUtils(unittest.TestCase): +class TestUtils: """Run tests to verify unpack independent of version.""" def test_unpack_v0x01_packet(self): """Test if the package in version v0x01 is properly unpacked.""" data = Hello_v0x01().pack() expected = unpack(data) - self.assertEqual(expected.pack(), data) + assert expected.pack() == data def test_unpack_v0x04_packet(self): """Test if the package in version v0x04 is properly unpacked.""" data = Hello_v0x04().pack() expected = unpack(data) - self.assertEqual(expected.pack(), data) + assert expected.pack() == data def test_invalid_packet_with_version_more_then_128(self): """Test validate a invalid packet with version more than 128.""" hello = Hello_v0x04() hello.header.version = 129 - self.assertRaises(UnpackException, validate_packet, hello.pack()) + pytest.raises(UnpackException, validate_packet, hello.pack()) def test_validate_packet_with_invalid_packets(self): """Test validate a invalid packet with invalid packets.""" hello = Hello_v0x04() hello.header.version = 128 - self.assertRaises(UnpackException, validate_packet, hello.pack()) + pytest.raises(UnpackException, validate_packet, hello.pack()) hello.header.version = 0 - self.assertRaises(UnpackException, validate_packet, hello.pack()) + pytest.raises(UnpackException, validate_packet, hello.pack()) diff --git a/tests/unit/v0x01/test_asynchronous/test_error_msg.py b/tests/unit/v0x01/test_asynchronous/test_error_msg.py index b6019c7a3..3296ba14b 100644 --- a/tests/unit/v0x01/test_asynchronous/test_error_msg.py +++ b/tests/unit/v0x01/test_asynchronous/test_error_msg.py @@ -8,7 +8,7 @@ class TestErrorMessage(TestStruct): """Test the Error Message.""" @classmethod - def setUpClass(cls): + def setup_class(cls): """Setup TestStruct.""" super().setUpClass() super().set_raw_dump_file('v0x01', 'ofpt_error_msg') @@ -30,4 +30,4 @@ def test_unpack_error_msg(self): actual = ErrorMsg(xid=24) actual.unpack(expected[8:]) - self.assertEqual(actual, error_msg) + assert actual == error_msg diff --git a/tests/unit/v0x01/test_common/test_flow_match.py b/tests/unit/v0x01/test_common/test_flow_match.py index 4c804c667..a48e1f6b0 100644 --- a/tests/unit/v0x01/test_common/test_flow_match.py +++ b/tests/unit/v0x01/test_common/test_flow_match.py @@ -1,13 +1,14 @@ """Testing FlowMatch structure.""" +import pytest import unittest from pyof.v0x01.common import flow_match -class TestMatch(unittest.TestCase): +class TestMatch: """Test Match structure.""" - def setUp(self): + def setup_method(self): """Basic setup for test.""" self.message = flow_match.Match() self.message.in_port = 22 @@ -25,21 +26,21 @@ def setUp(self): def test_get_size(self): """[Common/FlowMatch] - size 40.""" - self.assertEqual(self.message.get_size(), 40) + assert self.message.get_size() == 40 def test_pack_unpack(self): """[Common/FlowMatch] - packing and unpacking.""" pack = self.message.pack() unpacked = flow_match.Match() unpacked.unpack(pack) - self.assertEqual(self.message.pack(), unpacked.pack()) + assert self.message.pack() == unpacked.pack() - @unittest.skip('Not yet implemented') + @pytest.mark.skip('Not yet implemented') def test_pack(self): """[Common/FlowMatch] - packing.""" pass - @unittest.skip('Not yet implemented') + @pytest.mark.skip('Not yet implemented') def test_unpack(self): """[Common/FlowMatch] - unpacking.""" pass diff --git a/tests/unit/v0x01/test_common/test_header.py b/tests/unit/v0x01/test_common/test_header.py index e88b58a1b..8d5244036 100644 --- a/tests/unit/v0x01/test_common/test_header.py +++ b/tests/unit/v0x01/test_common/test_header.py @@ -1,4 +1,5 @@ """Testing Header structure.""" +import pytest import os import unittest from unittest.mock import patch @@ -6,10 +7,10 @@ from pyof.v0x01.common.header import Header, Type -class TestHeader(unittest.TestCase): +class TestHeader: """Test the message Header.""" - def setUp(self): + def setup_method(self): """Setup the TestHeader Class instantiating a HELLO header.""" self.message = Header() self.message.message_type = Type.OFPT_HELLO @@ -18,18 +19,18 @@ def setUp(self): def test_size(self): """[Common/Header] - size 8.""" - self.assertEqual(self.message.get_size(), 8) + assert self.message.get_size() == 8 - @unittest.expectedFailure + @pytest.mark.xfail def test_pack_empty(self): """[Common/Header] - packing empty header.""" - self.assertRaises(TypeError, + pytest.raises(TypeError, Header().pack()) def test_pack(self): """[Common/Header] - packing Hello.""" packed_header = b'\x01\x00\x00\x00\x00\x00\x00\x01' - self.assertEqual(self.message.pack(), packed_header) + assert self.message.pack() == packed_header def test_unpack(self): """[Common/Header] - unpacking Hello.""" @@ -38,9 +39,9 @@ def test_unpack(self): f = open(filename, 'rb') self.message.unpack(f.read(8)) - self.assertEqual(self.message.length, 8) - self.assertEqual(self.message.xid, 1) - self.assertEqual(self.message.message_type, Type.OFPT_HELLO) - self.assertEqual(self.message.version, 1) + assert self.message.length == 8 + assert self.message.xid == 1 + assert self.message.message_type == Type.OFPT_HELLO + assert self.message.version == 1 f.close() diff --git a/tests/unit/v0x01/test_common/test_phy_port.py b/tests/unit/v0x01/test_common/test_phy_port.py index dfdfe2818..fd2d6e568 100644 --- a/tests/unit/v0x01/test_common/test_phy_port.py +++ b/tests/unit/v0x01/test_common/test_phy_port.py @@ -1,17 +1,15 @@ """Testing PhyPort structure.""" import os -from unittest import TestCase - from pyof.foundation.basic_types import HWAddress from pyof.foundation.constants import OFP_MAX_PORT_NAME_LEN from pyof.v0x01.common.phy_port import ( PhyPort, PortConfig, PortFeatures, PortState) -class TestPhyPort(TestCase): +class TestPhyPort: """Test PhyPort.""" - def setUp(self): + def setup_method(self): """Basic setup for test.""" self.message = PhyPort() self.message.port_no = 1 @@ -23,14 +21,14 @@ def setUp(self): def test_get_size(self): """[Common/PhyPort] - size 48.""" - self.assertEqual(self.message.get_size(), 48) + assert self.message.get_size() == 48 def test_pack(self): """[Common/PhyPort] - packing.""" data = b'\x00\x01\x9a\xda\x11\x8a\xf4\x0cs1-eth1\x00\x00\x00\x00\x00' data += 15 * b'\x00' data += b'\xc0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' - self.assertEqual(self.message.pack(), data) + assert self.message.pack() == data def test_unpack(self): """[Common/PhyPort] - unpacking.""" @@ -40,11 +38,11 @@ def test_unpack(self): f.seek(16, 1) self.message.unpack(f.read(48)) - self.assertEqual(self.message.port_no, 1) - self.assertEqual(self.message.hw_addr, '9a:da:11:8a:f4:0c') - self.assertEqual(self.message.name, 's1-eth1') - self.assertEqual(self.message.state, PortState.OFPPS_STP_LISTEN) - self.assertEqual(self.message.curr, (PortFeatures.OFPPF_10GB_FD | - PortFeatures.OFPPF_COPPER)) + assert self.message.port_no == 1 + assert self.message.hw_addr == '9a:da:11:8a:f4:0c' + assert self.message.name == 's1-eth1' + assert self.message.state == PortState.OFPPS_STP_LISTEN + assert self.message.curr == (PortFeatures.OFPPF_10GB_FD | \ + PortFeatures.OFPPF_COPPER) f.close() diff --git a/tests/unit/v0x01/test_common/test_queue.py b/tests/unit/v0x01/test_common/test_queue.py index d8c184d62..bd3150302 100644 --- a/tests/unit/v0x01/test_common/test_queue.py +++ b/tests/unit/v0x01/test_common/test_queue.py @@ -1,13 +1,14 @@ """Testing Queue structure.""" +import pytest import unittest from pyof.v0x01.common import queue -class TestQueuePropHeader(unittest.TestCase): +class TestQueuePropHeader: """Test QueuePropHeader.""" - def setUp(self): + def setup_method(self): """Basic setup for test.""" self.message = queue.QueuePropHeader() self.message.queue_property = queue.QueueProperties.OFPQT_MIN_RATE @@ -15,23 +16,23 @@ def setUp(self): def test_get_size(self): """[Common/QueuePropHeader] - size 8.""" - self.assertEqual(self.message.get_size(), 8) + assert self.message.get_size() == 8 - @unittest.skip('Not yet implemented') + @pytest.mark.skip('Not yet implemented') def test_pack(self): """[Common/QueuePropHeader] - packing.""" pass - @unittest.skip('Not yet implemented') + @pytest.mark.skip('Not yet implemented') def test_unpack(self): """[Common/QueuePropHeader] - unpacking.""" pass -class TestPacketQueue(unittest.TestCase): +class TestPacketQueue: """TestPacketQueue.""" - def setUp(self): + def setup_method(self): """Basic setup for test.""" self.message = queue.PacketQueue() self.message.queue_id = 1 @@ -39,37 +40,37 @@ def setUp(self): def test_get_size(self): """[Common/PacketQueue] - size 8.""" - self.assertEqual(self.message.get_size(), 8) + assert self.message.get_size() == 8 - @unittest.skip('Not yet implemented') + @pytest.mark.skip('Not yet implemented') def test_pack(self): """[Common/PacketQueue] - packing.""" pass - @unittest.skip('Not yet implemented') + @pytest.mark.skip('Not yet implemented') def test_unpack(self): """[Common/PacketQueue] - unpacking.""" pass -class TestQueuePropMinRate(unittest.TestCase): +class TestQueuePropMinRate: """Test QueuePropMinRate.""" - def setUp(self): + def setup_method(self): """Basic setup for test.""" self.message = queue.QueuePropMinRate() self.message.rate = 1000 def test_get_size(self): """[Common/PropMinRate] - size 16.""" - self.assertEqual(self.message.get_size(), 16) + assert self.message.get_size() == 16 - @unittest.skip('Not yet implemented') + @pytest.mark.skip('Not yet implemented') def test_pack(self): """[Common/PropMinRate] - packing.""" pass - @unittest.skip('Not yet implemented') + @pytest.mark.skip('Not yet implemented') def test_unpack(self): """[Common/PropMinRate] - unpacking.""" pass diff --git a/tests/unit/v0x01/test_controller2switch/test_packet_out.py b/tests/unit/v0x01/test_controller2switch/test_packet_out.py index 397352661..626a10f48 100644 --- a/tests/unit/v0x01/test_controller2switch/test_packet_out.py +++ b/tests/unit/v0x01/test_controller2switch/test_packet_out.py @@ -1,4 +1,5 @@ """Packet out message tests.""" +import pytest from pyof.foundation.exceptions import ValidationError from pyof.v0x01.common.action import ActionOutput from pyof.v0x01.common.phy_port import Port @@ -14,7 +15,7 @@ class TestPacketOut(TestStruct): """ @classmethod - def setUpClass(cls): + def setup_class(cls): """Configure raw file and its object in parent class (TestDump).""" super().setUpClass() super().set_raw_dump_file('v0x01', 'ofpt_packet_out') @@ -23,7 +24,7 @@ def setUpClass(cls): actions=_get_actions()) super().set_minimum_size(16) - def setUp(self): + def setup_method(self): """Run before every test.""" self.message = self.get_raw_object() @@ -32,7 +33,7 @@ def test_valid_virtual_in_ports(self): valid = (Port.OFPP_LOCAL, Port.OFPP_CONTROLLER, Port.OFPP_NONE) for in_port in valid: self.message.in_port = in_port - self.assertTrue(self.message.is_valid()) + assert self.message.is_valid() def test_invalid_virtual_in_ports(self): """Invalid virtual ports as defined in 1.0.1 spec.""" @@ -40,23 +41,23 @@ def test_invalid_virtual_in_ports(self): Port.OFPP_FLOOD, Port.OFPP_ALL) for in_port in invalid: self.message.in_port = in_port - self.assertFalse(self.message.is_valid()) - self.assertRaises(ValidationError, self.message.validate) + assert not self.message.is_valid() + pytest.raises(ValidationError, self.message.validate) def test_valid_physical_in_ports(self): """Physical port limits from 1.0.0 spec.""" max_valid = int(Port.OFPP_MAX.value) for in_port in (1, max_valid): self.message.in_port = in_port - self.assertTrue(self.message.is_valid()) + assert self.message.is_valid() def test_invalid_physical_in_port(self): """Physical port limits from 1.0.0 spec.""" max_valid = int(Port.OFPP_MAX.value) for in_port in (-1, 0, max_valid + 1, max_valid + 2): self.message.in_port = in_port - self.assertFalse(self.message.is_valid()) - self.assertRaises(ValidationError, self.message.validate) + assert not self.message.is_valid() + pytest.raises(ValidationError, self.message.validate) def _get_actions(): diff --git a/tests/unit/v0x04/test_asynchronous/test_packet_in.py b/tests/unit/v0x04/test_asynchronous/test_packet_in.py index f8ff56c9c..d9650fd94 100644 --- a/tests/unit/v0x04/test_asynchronous/test_packet_in.py +++ b/tests/unit/v0x04/test_asynchronous/test_packet_in.py @@ -1,4 +1,5 @@ """Packet in message tests.""" +import pytest from pyof.v0x04.asynchronous.packet_in import PacketIn, PacketInReason from pyof.v0x04.common.constants import OFP_NO_BUFFER from pyof.v0x04.common.flow_match import ( @@ -11,7 +12,7 @@ class TestPacketInRaw(TestStruct): """Test PacketIn using a dump file.""" @classmethod - def setUpClass(cls): + def setup_class(cls): """Configure raw file and its object in parent class (TestDump).""" super().setUpClass() super().set_raw_dump_file('v0x04', 'ofpt_packet_in') @@ -27,12 +28,12 @@ def test_valid_physical_in_port(self): try: msg = self.get_raw_dump().read() except FileNotFoundError: - raise self.skipTest('No raw dump file found.') + raise pytest.skip('No raw dump file found.') else: max_valid = int(PortNo.OFPP_MAX.value) - 1 msg = self.get_raw_object() if msg.in_port in (1, max_valid): - self.assertTrue(msg.is_valid()) + assert msg.is_valid() def _new_match(): diff --git a/tests/unit/v0x04/test_common/test_flow_match.py b/tests/unit/v0x04/test_common/test_flow_match.py index b5e8c3ad8..37e909d77 100644 --- a/tests/unit/v0x04/test_common/test_flow_match.py +++ b/tests/unit/v0x04/test_common/test_flow_match.py @@ -1,12 +1,11 @@ """Test OXM-related implementations.""" -from unittest import TestCase - +import pytest from pyof.foundation.exceptions import PackException, UnpackException from pyof.v0x04.common.flow_match import ( Match, MatchType, OxmClass, OxmOfbMatchField, OxmTLV) -class TestMatch(TestCase): +class TestMatch: """Test Match class.""" tlv1 = OxmTLV(oxm_class=OxmClass.OFPXMC_OPENFLOW_BASIC, @@ -25,19 +24,19 @@ def test_unpacked_pack(self): """ unpacked = Match() unpacked.unpack(self.match.pack()) - self.assertEqual(self.match, unpacked) + assert self.match == unpacked def test_pack_other_instance(self): """Test packing another Match instance by using the value argument.""" expected = self.match.pack() valued_pack = Match().pack(self.match) - self.assertEqual(expected, valued_pack) + assert expected == valued_pack -class TestOxmTLV(TestCase): +class TestOxmTLV: """Test OXM TLV pack and unpack.""" - def setUp(self): + def setup_method(self): """Instantiate an OXM TLV struct.""" self.tlv = OxmTLV(oxm_class=OxmClass.OFPXMC_OPENFLOW_BASIC, oxm_field=OxmOfbMatchField.OFPXMT_OFB_IN_PHY_PORT, @@ -49,7 +48,7 @@ def test_different_class_types(self): OxmClass.OFPXMC_EXPERIMENTER): self.tlv.oxm_class = oxm_class unpacked = self._create_from_pack() - self.assertEqual(oxm_class, unpacked.oxm_class) + assert oxm_class == unpacked.oxm_class def test_different_fields(self): """Pack, unpack the result and assert the values are equal.""" @@ -57,21 +56,21 @@ def test_different_fields(self): OxmOfbMatchField.OFPXMT_OFB_IPV6_EXTHDR): self.tlv.oxm_field = oxm_field unpacked = self._create_from_pack() - self.assertEqual(oxm_field, unpacked.oxm_field) + assert oxm_field == unpacked.oxm_field def test_hasmask_bit(self): """Pack, unpack the result and assert the values are equal.""" for oxm_hasmask in True, False: self.tlv.oxm_hasmask = oxm_hasmask unpacked = self._create_from_pack() - self.assertEqual(oxm_hasmask, unpacked.oxm_hasmask) + assert oxm_hasmask == unpacked.oxm_hasmask def test_different_values(self): """Pack, unpack the result and assert the values are equal.""" for oxm_value in b'', b'abc': self.tlv.oxm_value = oxm_value unpacked = self._create_from_pack() - self.assertEqual(oxm_value, unpacked.oxm_value) + assert oxm_value == unpacked.oxm_value def _create_from_pack(self): """Return a new instance by unpacking self.tlv.pack().""" @@ -83,7 +82,7 @@ def test_pack_overflowed_field(self): """Raise PackException if field is bigger than 7 bit.""" self.tlv.oxm_class = OxmClass.OFPXMC_EXPERIMENTER self.tlv.oxm_field = 2**7 - with self.assertRaises(PackException): + with pytest.raises(PackException): self.tlv.pack() def test_pack_invalid_field(self): @@ -93,7 +92,7 @@ def test_pack_invalid_field(self): """ self.tlv.oxm_class = OxmClass.OFPXMC_OPENFLOW_BASIC self.tlv.oxm_field = 42 - with self.assertRaises(PackException): + with pytest.raises(PackException): self.tlv.pack() def test_unpack_invalid_field(self): @@ -103,7 +102,7 @@ def test_unpack_invalid_field(self): """ field42 = b'\x80\x00T\x00' tlv = OxmTLV() - with self.assertRaises(UnpackException): + with pytest.raises(UnpackException): tlv.unpack(field42) def test_max_field_value(self): @@ -112,4 +111,4 @@ def test_max_field_value(self): self.tlv.oxm_field = 127 unpacked = OxmTLV() unpacked.unpack(self.tlv.pack()) - self.assertEqual(self.tlv, unpacked) + assert self.tlv == unpacked diff --git a/tests/unit/v0x04/test_controller2switch/test_group_mod.py b/tests/unit/v0x04/test_controller2switch/test_group_mod.py index 90c6686de..4017f4f31 100644 --- a/tests/unit/v0x04/test_controller2switch/test_group_mod.py +++ b/tests/unit/v0x04/test_controller2switch/test_group_mod.py @@ -1,6 +1,4 @@ """group_mod tests.""" -from unittest import TestCase - from pyof.v0x04.controller2switch.group_mod import GroupMod from pyof.v0x04.common.action import ( ActionExperimenterDefault, ActionSetField, ListOfActions) @@ -10,27 +8,26 @@ from pyof.v0x04.controller2switch.group_mod import ListOfBuckets -class TestGroupMod(TestCase): +class TestGroupMod: """group_mod tests.""" def test_min_size(self): """Test minimum struct size.""" - self.assertEqual(16, GroupMod().get_size()) + assert 16 == GroupMod().get_size() -class TestBucket(TestCase): +class TestBucket: """bucket tests.""" def test_min_size(self): """Test minimum struct size.""" - self.assertEqual(16, Bucket().get_size()) + assert 16 == Bucket().get_size() -class TestListBuckets(TestCase): +class TestListBuckets: - def setUp(self): + def setup_method(self): """Configure raw file and its object in parent class (TestDump).""" - super().setUp() self.oxmtlv1 = OxmTLV(oxm_class=OxmClass.OFPXMC_OPENFLOW_BASIC, oxm_field=OxmOfbMatchField.OFPXMT_OFB_METADATA, @@ -69,34 +66,34 @@ def test_bucket_list(self): unpacked_buckets = ListOfBuckets() unpacked_buckets.unpack(buff) - self.assertEqual(len(unpacked_buckets), 3) - self.assertEqual(unpacked_buckets[0].length, 48) - self.assertEqual(unpacked_buckets[0].weight, 1) - self.assertEqual(len(unpacked_buckets[0].actions), 2) - self.assertEqual(unpacked_buckets[0].actions[0].field.oxm_value, - self.oxmtlv1.oxm_value) - self.assertEqual(unpacked_buckets[0].actions[1].field.oxm_value, - self.oxmtlv2.oxm_value) - - self.assertEqual(unpacked_buckets[1].length, 80) - self.assertEqual(unpacked_buckets[1].weight, 2) - self.assertEqual(len(unpacked_buckets[1].actions), 4) - self.assertEqual(unpacked_buckets[1].actions[0].field.oxm_value, - self.oxmtlv1.oxm_value) - self.assertEqual(unpacked_buckets[1].actions[1].field.oxm_value, - self.oxmtlv2.oxm_value) - self.assertEqual(unpacked_buckets[1].actions[2].body, - self.action3.body) - self.assertEqual(unpacked_buckets[1].actions[3].body, - self.action4.body) - - self.assertEqual(unpacked_buckets[2].length, 48) - self.assertEqual(unpacked_buckets[2].weight, 3) - self.assertEqual(len(unpacked_buckets[2].actions), 2) - self.assertEqual(unpacked_buckets[2].actions[0].body, - self.action3.body) - self.assertEqual(unpacked_buckets[2].actions[1].body, - self.action4.body) + assert len(unpacked_buckets) == 3 + assert unpacked_buckets[0].length == 48 + assert unpacked_buckets[0].weight == 1 + assert len(unpacked_buckets[0].actions) == 2 + assert unpacked_buckets[0].actions[0].field.oxm_value == \ + self.oxmtlv1.oxm_value + assert unpacked_buckets[0].actions[1].field.oxm_value == \ + self.oxmtlv2.oxm_value + + assert unpacked_buckets[1].length == 80 + assert unpacked_buckets[1].weight == 2 + assert len(unpacked_buckets[1].actions) == 4 + assert unpacked_buckets[1].actions[0].field.oxm_value == \ + self.oxmtlv1.oxm_value + assert unpacked_buckets[1].actions[1].field.oxm_value == \ + self.oxmtlv2.oxm_value + assert unpacked_buckets[1].actions[2].body == \ + self.action3.body + assert unpacked_buckets[1].actions[3].body == \ + self.action4.body + + assert unpacked_buckets[2].length == 48 + assert unpacked_buckets[2].weight == 3 + assert len(unpacked_buckets[2].actions) == 2 + assert unpacked_buckets[2].actions[0].body == \ + self.action3.body + assert unpacked_buckets[2].actions[1].body == \ + self.action4.body def test_buckets_one_item(self): @@ -112,14 +109,14 @@ def test_buckets_one_item(self): unpacked_buckets = ListOfBuckets() unpacked_buckets.unpack(buff) - self.assertEqual(len(unpacked_buckets), 1) - self.assertEqual(unpacked_buckets[0].length, 48) - self.assertEqual(unpacked_buckets[0].weight, 1) - self.assertEqual(len(unpacked_buckets[0].actions), 2) - self.assertEqual(unpacked_buckets[0].actions[0].field.oxm_value, - self.oxmtlv1.oxm_value) - self.assertEqual(unpacked_buckets[0].actions[1].field.oxm_value, - self.oxmtlv2.oxm_value) + assert len(unpacked_buckets) == 1 + assert unpacked_buckets[0].length == 48 + assert unpacked_buckets[0].weight == 1 + assert len(unpacked_buckets[0].actions) == 2 + assert unpacked_buckets[0].actions[0].field.oxm_value == \ + self.oxmtlv1.oxm_value + assert unpacked_buckets[0].actions[1].field.oxm_value == \ + self.oxmtlv2.oxm_value def test_buckets_no_action(self): @@ -135,9 +132,9 @@ def test_buckets_no_action(self): unpacked_buckets = ListOfBuckets() unpacked_buckets.unpack(buff) - self.assertEqual(len(unpacked_buckets), 1) - self.assertEqual(unpacked_buckets[0].length, 48) - self.assertEqual(unpacked_buckets[0].weight, 1) - self.assertEqual(len(unpacked_buckets[0].actions), 1) - self.assertEqual(unpacked_buckets[0].actions[0].field.oxm_value, - self.oxmtlv1.oxm_value) + assert len(unpacked_buckets) == 1 + assert unpacked_buckets[0].length == 48 + assert unpacked_buckets[0].weight == 1 + assert len(unpacked_buckets[0].actions) == 1 + assert unpacked_buckets[0].actions[0].field.oxm_value == \ + self.oxmtlv1.oxm_value diff --git a/tests/unit/v0x04/test_controller2switch/test_meter_mod.py b/tests/unit/v0x04/test_controller2switch/test_meter_mod.py index e3dc24f51..17243bf61 100644 --- a/tests/unit/v0x04/test_controller2switch/test_meter_mod.py +++ b/tests/unit/v0x04/test_controller2switch/test_meter_mod.py @@ -1,46 +1,44 @@ """MeterMod tests.""" -from unittest import TestCase - from pyof.v0x04.controller2switch.meter_mod import ( MeterBandDrop, MeterBandDscpRemark, MeterBandExperimenter, MeterBandHeader, MeterMod) -class TestMeterMod(TestCase): +class TestMeterMod: """MeterMod test.""" def test_min_size(self): """Test minimum message size.""" - self.assertEqual(16, MeterMod().get_size()) + assert 16 == MeterMod().get_size() -class TestMeterBandHeader(TestCase): +class TestMeterBandHeader: """MeterBandHeader test.""" def test_min_size(self): """Test minimum message size.""" - self.assertEqual(12, MeterBandHeader().get_size()) + assert 12 == MeterBandHeader().get_size() -class TestMeterBandDrop(TestCase): +class TestMeterBandDrop: """MeterBandDrop test.""" def test_min_size(self): """Test minimum message size.""" - self.assertEqual(16, MeterBandDrop().get_size()) + assert 16 == MeterBandDrop().get_size() -class TestMeterBandDscpRemark(TestCase): +class TestMeterBandDscpRemark: """MeterBandDscpRemark test.""" def test_min_size(self): """Test minimum message size.""" - self.assertEqual(16, MeterBandDscpRemark().get_size()) + assert 16 == MeterBandDscpRemark().get_size() -class TestMeterBandExperimenter(TestCase): +class TestMeterBandExperimenter: """MeterBandExperimenter test.""" def test_min_size(self): """Test minimum message size.""" - self.assertEqual(16, MeterBandExperimenter().get_size()) + assert 16 == MeterBandExperimenter().get_size() diff --git a/tests/unit/v0x04/test_controller2switch/test_port_mod.py b/tests/unit/v0x04/test_controller2switch/test_port_mod.py index e898b94a6..1886c17eb 100644 --- a/tests/unit/v0x04/test_controller2switch/test_port_mod.py +++ b/tests/unit/v0x04/test_controller2switch/test_port_mod.py @@ -4,9 +4,9 @@ from pyof.v0x04.controller2switch.port_mod import PortMod -class TestPortMod(unittest.TestCase): +class TestPortMod: """PortMod test.""" def test_min_size(self): """Test minimum message size.""" - self.assertEqual(40, PortMod().get_size()) + assert 40 == PortMod().get_size() diff --git a/tests/unit/v0x04/test_controller2switch/test_table_mod.py b/tests/unit/v0x04/test_controller2switch/test_table_mod.py index 34d7ac329..040700741 100644 --- a/tests/unit/v0x04/test_controller2switch/test_table_mod.py +++ b/tests/unit/v0x04/test_controller2switch/test_table_mod.py @@ -5,13 +5,13 @@ from pyof.v0x04.controller2switch.table_mod import TableMod -class TestTableMod(unittest.TestCase): +class TestTableMod: """TableMod test.""" def test_min_size(self): """Test minimum message size.""" - self.assertEqual(16, TableMod().get_size()) + assert 16 == TableMod().get_size() def test_header_type(self): """Test header type.""" - self.assertEqual(Type.OFPT_TABLE_MOD, TableMod().header.message_type) + assert Type.OFPT_TABLE_MOD == TableMod().header.message_type diff --git a/tests/unit/v0x04/test_struct.py b/tests/unit/v0x04/test_struct.py index 8aebdefe5..30d82d55c 100644 --- a/tests/unit/v0x04/test_struct.py +++ b/tests/unit/v0x04/test_struct.py @@ -1,11 +1,12 @@ """Automate struct tests.""" +import pytest import unittest from pyof.v0x04.common.header import Header from pyof.v0x04.common.utils import new_message_from_header -class TestStruct(unittest.TestCase): +class TestStruct: """Run tests related to struct packing and unpacking. Test the lib with raw dump files from an OpenFlow switch. We assume the @@ -104,11 +105,11 @@ def _test_pack_unpack(self, *args, **kwargs): unpacked = new_message_from_header(header) unpacked.unpack(packed[header_size:]) - self.assertEqual(packed, unpacked.pack()) + assert packed == unpacked.pack() def test_minimum_size(self): """Test struct minimum size.""" if self._min_size is None: - raise self.skipTest('minimum size was not set.') + raise pytest.skip('minimum size was not set.') obj = TestStruct._msg_cls() - self.assertEqual(obj.get_size(), self._min_size) + assert obj.get_size() == self._min_size