[Tutor] Testing dymamically created methods
Hi all, I would like to use some existing tool like py.test or nose to run my tests, but I failed to do so. The problem is as follow. I have tests: ### test_methods.py ### def test_one(): assert 1 == 1 def test_two(): assert 1 == 1 # I have abstraction layer that keeps information about this test, like method name. It's simple JSON file. Then I have the test runner: ### test_runner.py ### def read_test_definition(): """read the JSON file and returns dict with test details""" def test_run(): my_test_data = read_test_definition() import test_methods for testid in my_test_data: my_method = my_test_data[testid] # here the 'my_method' is equal 'test_one' or 'test_two', hope it's clear.. test_method = getattr(test_methods, my_method) test_method() ### This code works without py.test or nosetests. For example if I use print instead of 'assert'. Both py.test and nosetests failed to execute this correctly. Or maybe they do execute it correctly, I just don't understand it..:) They both report only single test was executed. I would like to see test report for each method executed in 'for' loop. Is it possible? PS. Sorry for my ignorance, I just started to learn Python last week. Thanks, Thomas ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Testing dymamically created methods
On Tue, Jan 10, 2012 at 3:31 PM, Walter Prins wrote: > Hi, > > On 10 January 2012 12:15, Thomas Maier wrote: >> This code works without py.test or nosetests. For example if I use print >> instead of 'assert'. >> Both py.test and nosetests failed to execute this correctly. >> Or maybe they do execute it correctly, I just don't understand it..:) >> They both report only single test was executed. >> I would like to see test report for each method executed in 'for' loop. >> Is it possible? > > For nose, I *think* you can basically achieve what you want by turning > test_run() into a generator (by essentially replacing the call to the > test_method() with a suitable "yield" statement. See here: > http://readthedocs.org/docs/nose/en/latest/writing_tests.html#test-generators Works perfect. > Additionally you might also look at the TestLoader functionality for > taking further control over how and where your tests are loaded, e.g. > see for example nose.loader.loadTestsFromGenerator() or > nose.loader.loadTestsFromGeneratorMethod() (or indeed all the other > methods) here: > http://readthedocs.org/docs/nose/en/latest/api/loader.html Will try later. > HTH, my $0.02 worth, > > Walter Thank you very much! Thomas ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating dict of dict : similar to perl hash of hash
On Tue, Mar 6, 2012 at 8:19 PM, David Rock wrote: > * Abhishek Pratap [2012-03-06 09:50]: >> Hi Guys >> >> I am looking for a way to build dictionaries of dict in python. >> >> For example in perl I could do >> >> my $hash_ref = {}; >> $hash->{$a}->{$b}->{$c} = "value"; >> if (exists $hash->{$a}->{$b}->{$c} ){ print "found value"} >> >> Can I do something similar with dictionaries in Python. > > Absolutely. Python is very good at using nested dicts. > > dict = {} > dict['a'] ={} > dict['a']['b'] = {} > dict['a']['b']['c']= "value" > > > This is a bit brute force, but it illustrates that the intermediary keys > need to exist. ie, if you try to assign directly, it won't work: > > Type "help", "copyright", "credits" or "license" for more information. dict ={} dict['a']['b']['c'] = 'value' > Traceback (most recent call last): > File "", line 1, in > KeyError: 'a' > > Since the key 'a' doesn't exist, it throws an exception. > > Python is also more flexible than perl in nesting data because it > doesn't have to be the same data type. You can nest variables, lists, > dicts, etc all at the same level: > > dict = {} > dict['mylist'] = [1,2,3] > dict['mystring'] = 'string' > dict['mynum'] = 4 > dict['anotherdict'] = {} > dict['anotherdict']['anotherstring'] = 'string2' > Hi David, Mixed data types in nested data structure are possible in Perl as well: %hash = (); $hash{'mylist'} = [1,2,3]; $hash{'mystring'} = 'string'; $hash{'mynum'} = 4; $hash{'anotherhash'} = {}; $hash{'anotherhash'}{'anotherstring'} = 'string2'; Thomas ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor