-1

I have 4 modules. 1. run.py Here I'm doing initialization and setup of class A and Class B. And what I want to do is:

    try:
        x = x[0]()
    except TestFailure as tf:
        print tf.message

where due to for loop, x iterates over class A and Class B. Hence for first iteration, 1st it's doing initialization for class A and then it's doing it for B.

  1. Test.py Here I've class Test

    class Test(object):
        IS_TEST = False
        teardown_stack = []
        td=[]
        def __init__(self):
            print "in Test init"
            super(Test, self).__init__()
    
        def setup(self):
            print "\tin Test setup"
            #self.add_teardown(foo)
    

and now

class TestFailure (None, msg):
message = ""
    if self.msg:
        message = self.msg
    pass
  1. I have A.py

    class A(Test):
        IS_TEST = True
        def __init__(self):
            test_variable = 9;
            if test_variable < 0:
                raise TestFailure("test_variable is negative.")
            print "in A init"
            super(A, self).__init__()        
    
        def setup(self):
            print "in A setup"
    
  2. and last module I have is B.py

    class B(A):
        IS_TEST = True
        def __init__(self):
            test_variable = 9;
            if test_variable < 0:
                raise TestFailure("test_variable is negative.")
            print "in B init"
            super(B, self).__init__()        
    
        def setup(self):
            print "\tin B setup"
    

I haven't used python exceptions before and hence I'm not familiar with them at all. How to get this work? Where exactly I'm going wrong? I want to raise exception by this method where before doing initialization, it makes the operation happen only when test_variable is positive.

ashwinjv
  • 2,787
  • 1
  • 23
  • 32
NikAsawadekar
  • 57
  • 1
  • 5

1 Answers1

0

I am unable to understand the flow of your scripts, but as far as your question regarding exception class, why not just inherit the Exception class when you create your exception class?

class TestFailure(Exception):
    pass

The rest comes from inheritance. You can find more information here: Proper way to declare custom exceptions in modern Python?

Community
  • 1
  • 1
ashwinjv
  • 2,787
  • 1
  • 23
  • 32