Dynamic Python Unit Test
Question: I am new to python unit testing. I have been playing with unittest and
py.test. I want to validate data in that is ini format. An example

    
    
    [Section1]
    key1 = value1
    key2 = value2
    key3 = value3
    ...
    [Section2]
    key1 = value1
    ...
    

The section names could be different in the ini file. I have data and I
compare it to the section/keynames.

I'm trying to create a test case for each section so I can generate a report
with failures by section name and which key/value is wrong. I research by I am
totally stuck.


Answer: Where gold.cfg is:

> [Section1]  
>  key1 = value1  
>  key2 = value2  
>  key3 = value3  
>  
>  [Section2]  
>  key4 = value4  
>  key5 = value5  
>  key6 = value6  
>  
>  [Section3]  
>  key7 = value7  
>  key8 = value8  
>  key9 = value9  
>

And where example.cfg is:

> [Section1]  
>  key1 = value1  
>  key2 = value2  
>  key3 = value3  
>  
>  [Section2]  
>  key1 = value1  
>
    
    
    # -*- coding: utf-8 -*-
    import unittest
    
    try:
        import ConfigParser as configparser  # Python 2
    except ImportError:
        import configparser  # Python 3
    
    
    class TestValidConfig(unittest.TestCase):
        def setUp(self):
            self.gold_config = configparser.RawConfigParser()
            self.gold_config.read('gold.cfg')
            self.allowed_section_names = self.gold_config.sections()
            return None
    
        def _test_allowed_section_names_pass(self):
            example_config = configparser.RawConfigParser()
            example_config.read('example.cfg')
            for section_name in example_config.sections():
                self.assertTrue(section_name in self.allowed_section_names)
            return None
    
        def test_values_by_section_pass(self):
            """Test example using setUp()"""
            example_config = configparser.RawConfigParser()
            example_config.read('example.cfg')
    
            for section_name in example_config.sections():
                example_pairs = example_config.items(section_name)
                gold_pairs = self.gold_config.items(section_name)
                self.assertTrue(set(example_pairs).issubset(set(gold_pairs)))
            return None
    
    if __name__ == '__main__':
        unittest.main()
    

Iterate over the section named with the sections() method: `for section_name
in example_config.sections()`.

The items() method returns a list of key, value tuples, so use set to assert
that the list of key, value pairs from the example file is a
[subset](https://docs.python.org/2/library/stdtypes.html#set.issubset) of the
key, value pairs from your gold copy.

(If the gold copy has a single section listed all allowed key/value pairs,
modify the code so that section name is used explicitly during the
comparison.)

