1

I come from a C++ background, my Python knowledge is very limited, I need help with the following situation:

Background:

We have a software SF, which is integrated into a large system S, this system S uses python unittest2 as the testing framework (however tweaked). Specifically, it implements a class A, which inherits from unittest2 and another customized exception handling class B. All test are then implemented based on A. System S is available on Linux only. However, this software SF is also used as a standalone application, which should only use unittest2 when we test it on other platforms, in which cases class A is not available.

Question:
How could I apply different test packages on different platforms?

My possible solution:
I am thinking of implementing a wrapper class based on this thread: Create a wrapper class to call a pre and post function around existing functions?. Answer from that post is put below:

class Wrapper(object):
    def __init__(self,wrapped_class):
        self.wrapped_class = wrapped_class()

    def __getattr__(self,attr):
        orig_attr = self.wrapped_class.__getattribute__(attr)
        if callable(orig_attr):
            def hooked(*args, **kwargs):
                self.pre()
                result = orig_attr(*args, **kwargs)
                # prevent wrapped_class from becoming unwrapped
                if result == self.wrapped_class:
                    return self
                self.post()
                return result
            return hooked
        else:
            return orig_attr    

However, I don't think the above thread is quite relevant since it is dealing with wrappers around class' member functions while I want some wrapper class that wrappers around different testing packages. Is it possible to do this in Python? I am using Python 2.7 FYI.

Any input is greatly appreciated and I am happy to add more information if it helps to make myself clear. Thank you.

Community
  • 1
  • 1
taocp
  • 23,276
  • 10
  • 49
  • 62
  • I'd use `nose` and its ["test packages"](https://nose.readthedocs.org/en/latest/writing_tests.html#test-packages) (or the test modules). Create a package for universal tests, and one for Linux-specific tests, then when the class is initialised, set its [`__test__`](https://nose.readthedocs.org/en/latest/finding_tests.html) attribute to the appropriate value. You could also use test skipping when the system is unavailable. Disclaimer: this is based mostly on skimming the nose docs briefly, but I think it should cover your needs. – millimoose Jul 11 '13 at 00:42
  • @millimoose thanks for your input, but we cannot use `nose`. I will take a look at that and hopefully I can borrow some ideas from it. – taocp Jul 11 '13 at 00:45
  • It seems that `unittest` supports test skipping natively: http://docs.python.org/2/library/unittest.html#skipping-tests-and-expected-failures, so it should be available in `unittest2`. Analogously, you could bundle per-platform tests in a [`TestSuite`](http://docs.python.org/2/library/unittest.html#unittest.TestSuite) and only run the appropriate suites in your test launcher. – millimoose Jul 11 '13 at 08:27

0 Answers0