On 10/14/2013 04:28 PM, Tomas Babej wrote:
Hi,

When trying to create a new ordered test case by inheriting
from already defined test case, by overriding few of its methods,
the execution order of the tests is as follows:
     - first all non-overriden test methods from the parent test class
     - then all overriden tests methods

That is not strictly true: if the base class is defined in a different file, with larger line numbers, with this patch its tests would run last.

This patch makes sure that methods are executed in the logical order,
that is, the order defined in the parent class.

We'll also need to support longer inheritance chains than just one parent.
The sort should look at the first @ordered superclass where the method was defined, and run the methods defined at the beginning of the inheritance chain first.

I've modified your patch to accomplish this, does it look OK?

--
PetrĀ³
From de0bcf5a3ba1e715bac44e4c3007fa8c6a4bdbbf Mon Sep 17 00:00:00 2001
From: Tomas Babej <tba...@redhat.com>
Date: Mon, 14 Oct 2013 14:28:24 +0200
Subject: [PATCH] ipatests: Extend the order plugin to properly handle
 inheritance

When trying to create a new ordered test case by inheriting
from already defined test case, by overriding few of its methods,
the execution order of the tests is as follows:
    - first all non-overriden test methods from the parent test class
    - then all overriden tests methods

This patch makes sure that methods are executed in the logical order,
that is, the order defined in the parent class.
---
 ipatests/order_plugin.py | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/ipatests/order_plugin.py b/ipatests/order_plugin.py
index 9ecff32adb23249bb152b031654e2f0883826e57..c09d4db2d185b6750e3c35cce86d98fd80f7e342 100644
--- a/ipatests/order_plugin.py
+++ b/ipatests/order_plugin.py
@@ -69,8 +69,27 @@ def wanted(attr):
                 return False
             return loader.selector.wantMethod(item)
 
+        def sort_with_respect_to_overriding(func):
+            """
+            Sorts the methods in respect with the parent class. If the method
+            is overriding a method from a parent ordered class, returns the
+            position of the method from the parent class.
+            """
+
+            # Check each *ordered* parent class for definition of func method
+            for i, parent_class in enumerate(reversed(cls.mro())):
+                if getattr(parent_class, '_order_plugin__ordered', False):
+                    method = getattr(parent_class, func.__name__, None)
+                    if method:
+                        # If it was defined, return the position of the parent
+                        # function
+                        return 0, i, method.func_code.co_firstlineno
+
+            # Method not found in superclasses, run it last
+            return 1, func.func_code.co_firstlineno
+
         methods = [getattr(cls, case) for case in dir(cls) if wanted(case)]
-        methods.sort(key=lambda m: m.func_code.co_firstlineno)
+        methods.sort(key=sort_with_respect_to_overriding)
         cases = [loader.makeTest(m, cls) for m in methods]
         return cases
 
-- 
1.8.3.1

_______________________________________________
Freeipa-devel mailing list
Freeipa-devel@redhat.com
https://www.redhat.com/mailman/listinfo/freeipa-devel

Reply via email to