[py-dev] Re: [py-svn] r12006 - in py/dist/py/test: . terminal

Jan Balster jan at balster.info
Tue May 10 10:47:11 CEST 2005


holger krekel wrote:

> not before the end of the week, sorry (i am just leaving the door). 
> can you provide an example meanwhile? 
> 

Yes, and a simple hack to fix the problem. I think you will find a
cleaner/better solution :-)

The tests are ordered by their sort value, which is (code.path,
code.firstlineno), and they are stored in a dict(), which doesn't
preserve the order items were added. So tests with the same sort value
don't seem to be executed in the expected order, but that depends on the
implementation of dict.


The function assert_order_of_execution is always defined last, because
otherwise you don't get such a nice error message (test_list could be
empty).

--------------
First example:
--------------

The tests (list_append_x) should be executed from 0 to 2, but they are
executed from 2 to 0.

##############################################################

def test_order_of_execution_generator_different_codeline():
    test_list = []
    expected_list = range(3)

    def list_append_2():
        test_list.append(2)

    def list_append_1():
        test_list.append(1)

    def list_append_0():
        test_list.append(0)

    def assert_order_of_execution():
        print 'expected order', expected_list
        print 'got           ', test_list
        assert test_list == expected_list


    yield list_append_0
    yield list_append_1
    yield list_append_2
    yield assert_order_of_execution

##############################################################

Sample output:

    def assert_order_of_execution():
        print 'expected order', expected_list
        print 'got           ', test_list
E       assert test_list == expected_list
>       assert [2, 1, 0] == [0, 1, 2]

[/.../.../test_order_of_execution.py:38]
[modulepath: test_order_of_execution_generator_different_codeline[3]]
- - - - - - - - - - - - - - - - - - - - - - - - -  [3]: recorded stdout
- - - - - - - - - - - - - - - - - - - - - - - - -
expected order [0, 1, 2]
got            [2, 1, 0]


---------------
Second Example:
---------------

This test only works with len(expected_list) > 3, on Linux with Python
2.4 and Python 2.3.4. It depends on the implementation of dict.

####################################################################
def test_order_of_execution_generator_same_codeline():
    test_list = []
    expected_list = range(6)

    def list_append(item):
        test_list.append(item)

    def assert_order_of_execution():
        print 'expected order', expected_list
        print 'got           ', test_list
        assert test_list == expected_list

    for i in expected_list:
        #test_list.append will raise a TypeError
        #yield lambda x, l = test_list: l.append(x), i
        yield list_append, i
    yield assert_order_of_execution

####################################################################


Sample output:

   def assert_order_of_execution():
        print 'expected order', expected_list
        print 'got           ', test_list
E       assert test_list == expected_list
>       assert [4, 5, 2, 3, 0, 1] == [0, 1, 2, 3, 4, 5]

[/.../.../test_order_of_execution.py:12]
[modulepath: test_order_of_execution_generator_same_codeline[6]]
- - - - - - - - - - - - - - - - - - - - - - - - -  [6]: recorded stdout
- - - - - - - - - - - - - - - - - - - - - - - - -
expected order [0, 1, 2, 3, 4, 5]
got            [4, 5, 2, 3, 0, 1]



-----
Fix:
-----

Index: collect.py
===================================================================
--- collect.py  (revision 12142)
+++ collect.py  (working copy)
@@ -347,7 +347,7 @@
             if not callable(call):
                 raise TypeError("yielded test %r not callable" %(call,))
             name = "[%d]" % i
-            d[name] = self.Function(name, self, args, obj=call)
+            d[name] = self.Function(name, self, args, obj=call,
sort_value = i)
         return d

     def getcallargs(self, obj):



Index: item.py
===================================================================
--- item.py     (revision 12142)
+++ item.py     (working copy)
@@ -45,12 +45,13 @@
         and executing a Python callable test object.
     """
     state = SetupState()
-    def __init__(self, name, parent, args=(), obj=_dummy):
+    def __init__(self, name, parent, args=(), obj=_dummy, sort_value =
None):
         super(Function, self).__init__(name, parent)
         self.args = args
         if obj is not _dummy:
             self._obj = obj
-
+        self.sort_value = sort_value
+
     def __repr__(self):
         return "<%s %r>" %(self.__class__.__name__, self.name)

@@ -59,7 +60,9 @@
         return code.path, code.firstlineno

     def getsortvalue(self):
-        return self.getpathlineno()
+        if self.sort_value is None:
+            return self.getpathlineno()
+        return self.sort_value

     def run(self):
         self.state.prepare(self)











































More information about the py-dev mailing list