Tidied up the test module to make it more PEP8 compliant
1 files changed, 59 insertions(+), 68 deletions(-)

M tests/test_whoswho.py
M tests/test_whoswho.py +59 -68
@@ 16,14 16,14 @@ from io import StringIO
 import sys
 import tempfile
 
-import py.test
+import pytest
 import whoswho
 
 from lxml import etree
 
 
 class TestAddressBook(object):
-    "Unit tests for our AddressBook class"
+    """Unit tests for our AddressBook class"""
 
     def setup_method(self, method):
         addr_xml = """<?xml version="1.0"?><addressbook><person id="1" name="Test Person"><name><first>Test</first><middle>X</middle><surname>Person</surname></name><date-of-birth>2004-12-16</date-of-birth><address>123 The Street<city>Sydney</city><state>NSW</state><postalcode>2000</postalcode><country>Australia</country></address><phone type="home">+61 2 9999 0000</phone><phone type="mobile">+61 400 000999</phone><phone type="work">+61 2 0000 9999</phone><fax type="home">+61 2 4444 5555</fax><fax type="work">+61 2 5555 4444</fax><email type="home">test.person@home.com.au</email><email type="work">test.person@work.com</email><web type="personal">http://www.isp.com.au/~testperson</web><web type="work">http://www.work.com/</web></person></addressbook>"""

          
@@ 34,35 34,35 @@ class TestAddressBook(object):
         self.empty_xml_file = StringIO(empty_xml)
 
     def test_init(self):
-        "Creating an address book from a valid XML file like object should succeed"
+        """Creating an address book from a valid XML file like object should succeed"""
         self.doc = whoswho.AddressBook(self.addr_xml_file)
 
     def test_init_empty(self):
-        "Creating an address book from an empty XML file should succeed"
+        """Creating an address book from an empty XML file should succeed"""
         self.doc = whoswho.AddressBook(self.empty_xml_file)
 
     def test_init_no_file(self):
-        "Create an empty address book"
+        """Create an empty address book"""
         self.doc = whoswho.AddressBook()
 
     def test_write_no_file_name(self):
-        "Can't write our address book to an operating system file without a file name"
+        """Can't write our address book to an operating system file without a file name"""
         doc = whoswho.AddressBook(self.addr_xml_file)
         assert hasattr(doc, 'file_name') == False
 
     def test_empty_no_file_name(self):
-        "Writing an address book to file without a filename should raise IOError"
+        """Writing an address book to file without a filename should raise IOError"""
         empty_ab = whoswho.AddressBook()
-        py.test.raises(IOError, empty_ab.write)
+        pytest.raises(IOError, empty_ab.write)
 
     def test_init_incorrect(self):
-        "Make sure that parsing an incorrect XML file fails"
+        """Make sure that parsing an incorrect XML file fails"""
         import xml
-        py.test.raises(etree.XMLSyntaxError, whoswho.AddressBook, self.badXmlFile)
+        pytest.raises(etree.XMLSyntaxError, whoswho.AddressBook, self.badXmlFile)
 
 
 class TestAddressBookWrite(object):
-    "Unit tests for the file writing methods of our AddressBook class"
+    """Unit tests for the file writing methods of our AddressBook class"""
 
     def setup_method(self, method):
         self.addr_xml = """<?xml version="1.0"?><addressbook><person id="1" name="Test Person"><name><first>Test</first><middle>X</middle><surname>Person</surname></name><date-of-birth>2004-12-16</date-of-birth><address>123 The Street<city>Sydney</city><state>NSW</state><postalcode>2000</postalcode><country>Australia</country></address><phone type="home">+61 2 9999 0000</phone><phone type="mobile">+61 400 000999</phone><phone type="work">+61 2 0000 9999</phone><fax type="home">+61 2 4444 5555</fax><fax type="work">+61 2 5555 4444</fax><email type="home">test.person@home.com.au</email><email type="work">test.person@work.com</email><web type="personal">http://www.isp.com.au/~testperson</web><web type="work">http://www.work.com/</web></person></addressbook>"""

          
@@ 74,14 74,14 @@ class TestAddressBookWrite(object):
         temp_xml_file.write(self.addr_xml)
 
     def test_open_from_file(self, tmpdir):
-        "Can we read the address book that we have just written?"
+        """Can we read the address book that we have just written?"""
         temp_xml_file = tmpdir.join(self.file_name)
         temp_xml_file.write(self.addr_xml)
         address_book = whoswho.AddressBook(temp_xml_file.strpath)
 
 
 class TestContact(object):
-    "Unit tests for the Contact class"
+    """Unit tests for the Contact class"""
 
     def setup_class(self):
         no_name_element = StringIO(

          
@@ 141,10 141,10 @@ class TestContact(object):
 
 
 class TestFind(object):
-    "Base test case for the find functions in whoswho"
+    """Base test case for the find functions in whoswho"""
 
     def setup_class(self):
-        "Create the whoswho xml list that forms our test data"
+        """Create the whoswho xml list that forms our test data"""
         addr_xml = """<?xml version="1.0"?><addressbook>
         <person id="1" name="Someone Else">
          <category>friend</category>

          
@@ 197,15 197,15 @@ class TestFind(object):
 
 
 class TestFindName(TestFind):
-    "Unit tests for whoswho.find_name"
+    """Unit tests for whoswho.find_name"""
 
     def test_success(self):
-        "Test that finding a person who is in our file succeeds"
+        """Test that finding a person who is in our file succeeds"""
         count = whoswho.find_name(self.address_book, "Person")
         assert len(count) == 1
 
     def test_success_full(self):
-        "Test that full find produces the appropriate output"
+        """Test that full find produces the appropriate output"""
         results = whoswho.find_name(self.address_book, "Person")
         for contact in results:
             assert 'Address:' in contact.details(), "Full contact details don't include an address"

          
@@ 213,12 213,12 @@ class TestFindName(TestFind):
             assert 'Acme Widgets' in contact.details(), "Company name missing from full name results"
 
     def test_unsuccessful(self):
-        "Test that searching for a non-existent person returns no results"
+        """Test that searching for a non-existent person returns no results"""
         count = whoswho.find_name(self.address_book, "Nobody")
         assert len(count) == 0
 
     def test_empty(self):
-        "Test that searching for anything in an empty address book returns no results"
+        """Test that searching for anything in an empty address book returns no results"""
         count = whoswho.find_name(self.empty_address_book, "Person")
         assert len(count) == 0
 

          
@@ 278,12 278,12 @@ class TestFindName(TestFind):
 
 
 class TestFindId(TestFind):
-    "Unit tests for whoswho.find_by_id"
+    """Unit tests for whoswho.find_by_id"""
 
     def test_success(self):
-        "Test that finding a person who is in our file succeeds"
+        """Test that finding a person who is in our file succeeds"""
         results = whoswho.find_by_id(self.address_book, 1)
-        assert results != None
+        assert results is not None
 
     def test_success_full(self):
         """Test that full find on a person who is in our file produces appropriate output

          
@@ 299,41 299,41 @@ class TestFindId(TestFind):
             assert person != []
 
     def test_unsuccessful(self):
-        "Test that searching for a non-existent person returns no results"
+        """Test that searching for a non-existent person returns no results"""
         results = whoswho.find_by_id(self.address_book, 2)
         assert results is None
 
     def test_empty(self):
-        "Test that searching for anything in an empty address book returns no results"
+        """Test that searching for anything in an empty address book returns no results"""
         results = whoswho.find_by_id(self.empty_address_book, 0)
         assert results is None
 
 
 class TestFindCompany(TestFind):
-    "Unit tests for whoswho.find_by_tag looking for company names"
+    """Unit tests for whoswho.find_by_tag looking for company names"""
 
     def test_success(self):
-        "Test that finding a company that is in our file succeeds"
+        """Test that finding a company that is in our file succeeds"""
         results = whoswho.find_by_tag(self.address_book, 'company/name', 'Acme Widgets')
         assert results is not None
 
     def test_unsuccessful(self):
-        "Test that finding a non-existent company returns no results"
+        """Test that finding a non-existent company returns no results"""
         results = whoswho.find_by_tag(self.address_book, 'company/name', 'Oakton')
         assert len(results) == 0
 
     def test_full(self):
-        "Test that company name is included in full output"
+        """Test that company name is included in full output"""
         test_company = 'Acme Widgets'
         results = whoswho.find_by_tag(self.address_book, 'company/name', test_company)
         assert test_company in results[0].details()
 
 
 class TestMyAddressBook(object):
-    "Unit tests specific to my address book. Not to be distributed"
+    """Unit tests specific to my address book. Not to be distributed"""
 
     def setup_class(self):
-        "Create the address book that forms our test data"
+        """Create the address book that forms our test data"""
         config_file = whoswho.get_config_file()
         ab_file_names = whoswho.get_ab_filenames(config_file)
         self.address_books = [whoswho.AddressBook(x.strip()) for x in ab_file_names]

          
@@ 360,10 360,10 @@ class TestMyAddressBook(object):
 
 
 class TestAdd(object):
-    "Unit tests for adding a Contact to an AddressBook"
+    """Unit tests for adding a Contact to an AddressBook"""
 
     def setup_class(self):
-        "Create the whoswho xml list that forms our test data"
+        """Create the whoswho xml list that forms our test data"""
         self.addr_xml = """<?xml version="1.0"?><addressbook>
         <person id="999" name="Test Person"><name><first>Test</first><middle>X</middle><surname>Person</surname></name><date-of-birth>2004-12-16</date-of-birth><address>123 The Street<city>Sydney</city><state>NSW</state><postalcode>2000</postalcode><country>Australia</country></address><phone type="home">+61 2 9999 0000</phone><phone type="mobile">+61 400 000999</phone><phone type="work">+61 2 0000 9999</phone><fax type="home">+61 2 4444 5555</fax><fax type="work">+61 2 5555 4444</fax><email type="home">test.person@home.com.au</email><email type="work">test.person@work.com</email><web type="personal">http://www.isp.com.au/~testperson</web><web type="work">http://www.work.com/</web></person>
         <person id="998" name="Minimal A. Entry"><name><first>Minimal</first><surname>Entry</surname></name></person>

          
@@ 374,27 374,27 @@ class TestAdd(object):
         self.address_book = whoswho.AddressBook(self.addr_xml_file)
 
     def test_add(self):
-        "Test that adding a valid new person succeeds"
+        """Test that adding a valid new person succeeds"""
         start = len(self.address_book.contacts)
-        myPerson = whoswho.Contact()
-        myPerson.id = 1
-        myPerson.name = "Minimal 3 Entry"
-        self.address_book.add(myPerson)
+        my_person = whoswho.Contact()
+        my_person.id = 1
+        my_person.name = "Minimal 3 Entry"
+        self.address_book.add(my_person)
         finish = len(self.address_book.contacts)
         assert start + 1 == finish, "Adding a contact has failed"
 
     def test_add_no_name(self):
-        "Test that we can't add a Contact without a name"
+        """Test that we can't add a Contact without a name"""
         my_person = whoswho.Contact()
         my_person.id = 2
-        py.test.raises(AttributeError, self.address_book.add, my_person)
+        pytest.raises(AttributeError, self.address_book.add, my_person)
 
     def test_no_duplicate_ids(self):
-        "Test that we can't add a Contact with an ID that is already in our address book"
+        """Test that we can't add a Contact with an ID that is already in our address book"""
         my_person = whoswho.Contact()
         my_person.id = 999
         my_person.name = 'Duplicate id person'
-        py.test.raises(IndexError, self.address_book.add, my_person)
+        pytest.raises(IndexError, self.address_book.add, my_person)
 
 
 class TestSchemaValidation(object):

          
@@ 405,27 405,19 @@ class TestSchemaValidation(object):
 
     def setup_class(self):
         self.config_file = whoswho.get_config_file()
-        empty_xml = """<?xml version="1.0"?><addressbook></addressbook>"""
-        self.empty_xml_file = StringIO(empty_xml)
-
-    def no_test_get_schema_file(self):
-        "Is there a schema file in the standard config file?"
-        self.schema_file = whoswho.get_config_item(self.config_file, 'whoswho', 'schemafile')
 
     def test_valid_schema(self):
-        """Is the schema file in the config file a parseable RelaxNG schema?
-
-        Relies on test_get_schema_file having run first
+        """Is the schema file a parseable RelaxNG schema?
         """
-        # self.schema_file = whoswho.get_config_item(self.config_file, 'whoswho', 'schemafile')
-        # self.rng_schema = etree.RelaxNG(etree.parse(self.schema_file))
         self.rng_schema = etree.RelaxNG(etree.parse('whoswho.rng'))
 
 
-def TestOtherSchemas(self):
+class TestOtherSchemas():
+    """Unit tests for schema validation using our RelaxNG schema"""
+
     def setup_class(self):
-        schema_file = whoswho.get_config_item(self.config_file, 'whoswho', 'schemafile')
-        self.rng_schema = etree.RelaxNG(etree.parse(schema_file))
+        self.config_file = whoswho.get_config_file()
+        self.rng_schema = etree.RelaxNG(etree.parse('whoswho.rng'))
 
     def test_lxml_schema_validation(self):
         """Validate that my address book is valid according to our schema

          
@@ 440,12 432,11 @@ def TestOtherSchemas(self):
         assert success == True
 
     def test_empty_file(self):
-        """An empty XML file does conform to our schema so validation should
-        succeed
-
-        Relies on test_valid_schema having run first
+        """An empty XML file does conform to our schema so validation should succeed
         """
-        document = etree.parse(self.empty_xml_file)
+        empty_xml = """<?xml version="1.0"?><addressbook name=''></addressbook>"""
+        empty_xml_file = StringIO(empty_xml)
+        document = etree.parse(empty_xml_file)
         assert self.rng_schema.validate(document) == True
 
 

          
@@ 461,7 452,7 @@ class TestGetAddressBookFileName(object)
         self.valid_config = """\n[whoswho]\naddressbook = an_address_file_name\nschemafile = a_schema_file_name"""
 
     def setup_method(self, method):
-        "Ensure that the file is reset for each method that uses it"
+        """Ensure that the file is reset for each method that uses it"""
         self.config = StringIO(self.valid_config)
 
     def test_valid_config_file(self):

          
@@ 486,18 477,18 @@ class TestMain(object):
     contents change."""
 
     def setup_class(self):
-        "Redirect standard output for the duration of the test"
+        """Redirect standard output for the duration of the test"""
         self.stdout = sys.stdout
         sys.stdout = StringIO()
 
     def tearDown(self):
-        "Reinstate standard output"
+        """Reinstate standard output"""
         sys.stdout = self.stdout
 
     def test_minus_f(self):
         arguments = ['whoswho.py', '-f', 'Todd']
-        returnValue = whoswho.main(arguments)
-        assert returnValue == True
+        return_value = whoswho.main(arguments)
+        assert return_value == True
 
     def test_minus_i(self):
         arguments = ['whoswho.py', '-i', '1']

          
@@ 510,5 501,5 @@ class TestMain(object):
         My address book should have at least one company defined
         """
         arguments = ['whoswho.py', '-m', 'Lend']
-        returnValue = whoswho.main(arguments)
-        assert returnValue == True
+        return_value = whoswho.main(arguments)
+        assert return_value == True
  No newline at end of file