1

I have to test few sites which sales the same things, but they have an another template.

So I want to run each MainTestClass with giving some input parameter, let's say :

java -jar SeleniumServerStandalone-2.0b2.jar -port 5555 (template_id=5)

Is it possible?

class MainTestCases(unittest.TestCase):
    def setUp(self):

        #self.template_id=template_id I want something like that
        self.verificationErrors = []

        self.selenium = selenium("localhost", 5555, "*chrome", "http://www.google.com/")
        time.sleep(5)
        self.selenium.start()


    def test_test1(self):
        if self.template_id==1:
        ...
        elif self.template_id==2:
        ...
    def test_test2(self):
        if self.template_id==1:
        ...
        elif self.template_id==2:
        ...
    def tearDown(self):
        self.selenium.stop()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()
1

2 Answers 2

1

Try adding an init method to MainTestCases, like so:

class MainTestCases(unittest.TestCase):

    def __init__(self, methodName, template_id):
        super(MainTestCases, self).__init__(self, methodName)
        self.template_id = templateId

    def setUp(self):
        ... and so on...

Due to this customization, you will need to build your test suite manually because each test case has to be instantiated with the template_id, like so--

def suite(template_id):
    testcasenames = unittest.defaultTestLoader.loadTestsFromTestCase(MainTestCases)
    suite = []
    for case in testcasename:
        suite.append(MainTestCases(case, template_id)
    return suite

Then in main, instead of unittest.main(), do:

  1. Parse command-line arguments. You may want to consider the argparse (2.7+) or optparse (2.6 and earlier) modules. They are powerful, but easy to start with by looking at the examples.
  2. Create and run the suite: unittest.TextTestRunner().run(suite(template_id))
Sign up to request clarification or add additional context in comments.

Comments

0

Now, I use this solution:

  1. Create suite test which runs testcases:

import unittest from Flights.FlightsTestCases import FlightsTestCases import sys from Flights.FlightTemplate import FlightTemplate

def suite():

    testSuite= unittest.TestSuite()
    testSuite.addTest(FlightsTestCases('test_test1'))


    FlightsTestCases.www_address='http://testpage.pl/'
    FlightsTestCases.flight_template=FlightTemplate.Test

    #FlightsTestCases.www_address='http://productionpage.pl/'
    #FlightsTestCases.flight_template=FlightTemplate.Production

    return testSuite


if __name__ == "__main__":
    result = unittest.TextTestRunner(verbosity=2).run(suite())
    sys.exit(not result.wasSuccessful())

change set_up to something like:

class FlightsTestCases(unittest.TestCase): www_address = None flight_template = None xml_report_generator = None

def setUp(self):
    self.verificationErrors = []
    if self.www_address == None:
        self.selenium = selenium("localhost", 5555, "*chrome", "http://testpage.pl/")
    else:
        self.selenium = selenium("localhost", 5555, "*chrome", self.www_address)

1 Comment

This answer demands formatting, but I can't find how to edit it :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.