Spot any errors? let me know, but Unleash your pedant politely please.

Thursday, 20 December 2012

Being a bit lazy, I have some modules that I use that I haven't packaged. I just copy them from project to project.  One is a Java style properties thingamajig. (This is probably unPythonic, there are probably better versions of the same thing out there, but it works for me).  I have a small test in the __main__ that requires a file containing some test data.

Being a bit lazy, I got fed up of copying that test file. It occurred to me today that I should write the test file as part of the test and remove it afterwards.

Why didn't I think of this before?

if __name__ == '__main__':
    
    open('test.properties','w').write("""Param : This is a parameter value
                                          Multi Param  : multi parameter 1
                                         +Multi Param : multi parameter 2
                                         +Multi Param :  multi parameter 3""")
    
    test_properties = Properties('test.properties')
    assert test_properties.property('Param')=='This is a parameter value'
    assert test_properties.propertyAsList('Multi Param')==['multi parameter 1','multi parameter 2','multi parameter 3']
    os.remove('test.properties')


I guess the tests should be put into some kind of unit test framework, but I can't quite yet be arsed to figure that stuff out. If this horrifies you, please do point me in the right direction.

No comments:

Post a Comment