22import unittest
33
44import canopen
5+ import canopen .network
56
67from .util import SAMPLE_EDS
78
@@ -14,15 +15,13 @@ def count_subscribers(network: canopen.Network) -> int:
1415
1516
1617class TestLocalNode (unittest .TestCase ):
17- """
18- Test local node.
19- """
18+ """Unit tests for the LocalNode class."""
2019
2120 @classmethod
2221 def setUpClass (cls ):
2322 cls .network = canopen .Network ()
2423 cls .network .NOTIFIER_SHUTDOWN_TIMEOUT = 0.0
25- cls .network .connect ("test" , interface = "virtual" )
24+ cls .network .connect (interface = "virtual" )
2625
2726 cls .node = canopen .LocalNode (2 , SAMPLE_EDS )
2827
@@ -46,11 +45,10 @@ def test_associate_network(self):
4645 self .assertIs (self .node .nmt .network , self .network )
4746 self .assertIs (self .node .emcy .network , self .network )
4847
49- # Test that its possible to associate the network multiple times
50- # by checking that the number of subscribers remains the same
51- count = count_subscribers (self .network )
52- self .node .associate_network (self .network )
53- self .assertEqual (count_subscribers (self .network ), count )
48+ # Test that its not possible to associate the network multiple times
49+ with self .assertRaises (RuntimeError ) as cm :
50+ self .node .associate_network (self .network )
51+ self .assertIn ("already associated with a network" , str (cm .exception ))
5452
5553 # Test removal of the network. The count of subscribers should
5654 # be the same as before the association
@@ -69,15 +67,13 @@ def test_associate_network(self):
6967
7068
7169class TestRemoteNode (unittest .TestCase ):
72- """
73- Test remote node.
74- """
70+ """Unittests for the RemoteNode class."""
7571
7672 @classmethod
7773 def setUpClass (cls ):
7874 cls .network = canopen .Network ()
7975 cls .network .NOTIFIER_SHUTDOWN_TIMEOUT = 0.0
80- cls .network .connect ("test" , interface = "virtual" )
76+ cls .network .connect (interface = "virtual" )
8177
8278 cls .node = canopen .RemoteNode (2 , SAMPLE_EDS )
8379
@@ -100,11 +96,10 @@ def test_associate_network(self):
10096 self .assertIs (self .node .rpdo .network , self .network )
10197 self .assertIs (self .node .nmt .network , self .network )
10298
103- # Test that its possible to associate the network multiple times
104- # by checking that the number of subscribers remains the same
105- count = count_subscribers (self .network )
106- self .node .associate_network (self .network )
107- self .assertEqual (count_subscribers (self .network ), count )
99+ # Test that its not possible to associate the network multiple times
100+ with self .assertRaises (RuntimeError ) as cm :
101+ self .node .associate_network (self .network )
102+ self .assertIn ("already associated with a network" , str (cm .exception ))
108103
109104 # Test removal of the network. The count of subscribers should
110105 # be the same as before the association
0 commit comments