4

I am on a website where you login, then search for course, click on course and then it lands on the Credit card page, since its just one course and is common and I am just trying to run DDT on CC section and not search section this is what I did.

On the test page this is how my code looks like:

@pytest.mark.usefixtures("oneTimeSetUp","setUp")
@ddt
class SendformTest(unittest.TestCase):
    @pytest.fixture(autouse=True)
    def classSetup(self,oneTimeSetUp):
        self.sf = SendForms(self.driver)
        self.ts = TestStatus(self.driver)


    def setUp(self):  ############ I want this method to run just once after login, this method searches for course and then clicks and land on cc page
        self.sf.navigateToCCForms("img")

    @pytest.mark.run(order = 1)
    @data(*getCSVData("testRegular.csv"))
    @unpack
    def test_cc_Form(self,ccNum, ccExp, ccCVV):
        result = self.sf.verifyOnCCforms() 
        self.ts.mark(result,"On Send Form Page")
        self.sf.ccdetails(ccNum, ccExp, ccCVV)
        result1 = self.sf.sentSuccessMsg()
        self.ts.markFinal("test_sending_Form", result1, "this is test")

Right now this runs once fine and then just sits there, it wont enter data multiple times on the CC page. how do I make setUp method run just once. I tried:

@classmethod
def setUpClass(self):
    #super(SendformTest, self).setUpClass()
    self.sf.navigateToSendforms("img")

but getting the:

AttributeError: type object 'SendformTest' has no attribute 'sf'

  • 1
    Your setup and teardown methods should not reference 'self' but rather 'cls'. Here is the link to the unittest doc - docs.python.org/3/library/unittest.html – bmshort Feb 11 '17 at 11:21
  • I tried CLS but that didn't work – Ronron Feb 11 '17 at 14:01
  • Same error? What does setUpClass look like right now? – bmshort Feb 11 '17 at 14:07
1

The problem is that setUpClass is executed earlier than classSetup fixture, hence the AttributeError.

One workaround might be to manually allow this method to be executed once, something like:

def setUp(self):  
    if getattr(self, 'navigated'):
         return

    self.sf.navigateToCCForms("img")
    self.navigated = True

I would also try the setup_class() method supported by pytest, though I suspect you would get the same issue as with setUpClass.

0

You need to use setupClass to have something run once in the test class , setUp is run between each test. You don't use self in setupClass you use cls and need to bring it into the method. You also need to add the decorator to make it a class method not an instance method.

@classmethod def setupClass(cls): cls.sf = SendForms(self.driver) cls.ts = TestStatus(self.driver) cls.sf.navigateToSendforms("img")

  • anything you do at class level will be available as self when you are back in the methods and operating at instance level. – Amias Jan 26 '18 at 11:44
  • you might need to use the @classmethod decorator here but its not appropriate if you are using self because thats only for instances of the class. – Amias Jan 26 '18 at 11:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.