如何向 Selenium RC TestSuite 输入参数?

发布于 2024-10-21 18:13:26 字数 896 浏览 6 评论 0原文

我必须测试几个销售相同产品的网站,但他们有另一个模板。

所以我想运行每个 MainTestClass 并给出一些输入参数,比方说:

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

这可能吗?

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()

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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

失退 2024-10-28 18:13:26

尝试向 MainTestCases 添加一个 init 方法,如下所示:

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...

由于此自定义,您将需要手动构建测试套件,因为每个测试用例都必须使用 template_id 实例化,如下所示 -

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

然后ma​​in,而不是unittest.main(),做:

  1. 解析命令行参数。您可能需要考虑 argparse(2.7+)或 optparse(2.6 及更早版本)模块。它们功能强大,但通过查看示例很容易上手。
  2. 创建并运行套件:unittest.TextTestRunner().run(suite(template_id))

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))
日记撕了你也走了 2024-10-28 18:13:26

现在,我使用这个解决方案:

  1. 创建运行测试用例的套件测试:

导入unittest
从 Flights.FlightsTestCases 导入 FlightsTestCases
导入系统
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())

将 set_up 更改为:

class FlightsTestCases(unittest.TestCase):
www_地址 = 无
飞行模板 = 无
xml_report_generator = 无

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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文