I'm a Java/C coder getting started with some Python for a side-project. I've written some unit tests for a recent side-project I'm working on, but I have a sinking suspicion I'm just writing Java-code, translated into Python.
I've posted a small sample of some unit-tests I wrote. I'd really appreciate any advice in how to make the code more Pythonic (best-practices, conventions, etc).
import unittest
import urllib2
from connectionstate import ConnectionState
class Test(unittest.TestCase):
"""Unit tests for the ConnectionState"""
def test_connectionstate_reports_bad_network_connection(self):
"""ConnectionState reports a failure when currently disconnected"""
#Set up ConnectionState around mock
always_disconnected = mock_connection_check_func_always_disconnected
connstate_with_mock = ConnectionState(always_disconnected)
actual_state = connstate_with_mock.is_connected()
self.assertEqual(actual_state, False)
def test_connectionstate_detects_good_network_connection(self):
"""ConnectionState should report success when we can connect"""
#Set up ConnectionState around mock
always_connected = mock_connection_check_func_always_connected
connstate_with_mock = ConnectionState(always_connected)
actual_state = connstate_with_mock.is_connected()
self.assertEqual(actual_state, True)
def test_connectionstate_remembers_disconnections(self):
"""If the internet connection drops and then comes back, ConnectionState
can remember that the connection dropped temporarily
"""
#Set up ConnectionState around mock
sometimes_connected = mock_conn_every_other_conn_succeeds
connstate_with_mock = ConnectionState(sometimes_connected)
#Call is_connected a few times so get an unsuccessful connection
for count in range(0, 3):
connstate_with_mock.is_connected()
actual_result = connstate_with_mock.has_connection_dropped()
self.assertEqual(actual_result, True)
def test_connectionstate_doesnt_bring_up_ancient_history(self):
"""has_connection_dropped only reports failures that have happened
since the last call to has_connection_dropped. Calling
"""
#Set up ConnectionState around mock
sometimes_connected = mock_conn_every_other_conn_succeeds
connstate_with_mock = ConnectionState(sometimes_connected)
#Call is connected a few times so an unsuccessful connection is reported
for count in range(0, 3):
connstate_with_mock.is_connected()
#Call once to clear failures
connstate_with_mock.has_connection_dropped()
actual_result = connstate_with_mock.has_connection_dropped()
self.assertEqual(actual_result, False)
#Begin mock-connection functions
def mock_connection_check_func_always_disconnected(request=None, timeout=1):
raise urllib2.URLError("Unable to connect to network")
def mock_connection_check_func_always_connected(request=None, timeout=1):
pass
def mock_conn_every_other_conn_succeeds(request=None, timeout=1):
#Initialize 'static' call-counter variable
if "call_counter" not in mock_conn_every_other_conn_succeeds.__dict__:
mock_conn_every_other_conn_succeeds.call_counter = 0;
mock_conn_every_other_conn_succeeds.call_counter += 1
if mock_conn_every_other_conn_succeeds.call_counter % 2 == 0:
mock_connection_check_func_always_disconnected(request, timeout)
else:
mock_connection_check_func_always_connected(request, timeout)
#End mock-connection functions
if __name__ == "__main__":
unittest.main()