Ejemplo n.º 1
0
    def setUp(self):
        """Sets up the environment for a test case.

        Retreive already running test environment, or create a new one if
        one doesn't exist.  A test environment consists of a web
        server with HTTP and WebSocket handlers which tests can access
        through ``self.handler``.

        Then set up a Marionette session to the connected device if
        one does not already exist.  This is assigned to
        ``self.marionette``.  Marionette can be used to remote control
        the device.

        """

        super(TestCase, self).setUp()

        env = environment.get(InProcessTestEnvironment)

        try:
            if env.device_profile:
                self.check_skip(env.device_profile['webapi'])
        except:
            self.test_name = str(self.__class__).split('.')[1]

        self.server = env.server
        self.handler = env.handler

        self.assert_browser_connected()
        self.marionette = TestCase.create_marionette()

        if not certapp.is_installed():
            certapp.install(marionette=self.marionette, version=self.version)

        # Make sure we don't reuse the certapp context from a previous
        # testrun that was interrupted and left the certapp open.
        try:
            certapp.kill(self.marionette)
        except certapp.CloseError:
            self.close_app_manually()

        try:
            self.app = certapp.launch(self.marionette)
            self.showTestStatusInDevice(self.marionette)
        except certapp.LaunchError:
            self.launch_app_manually()
Ejemplo n.º 2
0
    def setUp(self):
        """Sets up the environment for a test case.

        Retreive already running test environment, or create a new one if
        one doesn't exist.  A test environment consists of a web
        server with HTTP and WebSocket handlers which tests can access
        through ``self.handler``.

        Then set up a Marionette session to the connected device if
        one does not already exist.  This is assigned to
        ``self.marionette``.  Marionette can be used to remote control
        the device.

        """

        super(TestCase, self).setUp()

        env = environment.get(InProcessTestEnvironment)

        try:
            if env.device_profile:
                self.check_skip(env.device_profile['webapi'])
        except:
            self.test_name = str(self.__class__).split('.')[1]

        self.server = env.server
        self.handler = env.handler

        self.assert_browser_connected()
        self.marionette = TestCase.create_marionette()

        if not certapp.is_installed():
            certapp.install(marionette=self.marionette, version=self.version)

        # Make sure we don't reuse the certapp context from a previous
        # testrun that was interrupted and left the certapp open.
        try:
            certapp.kill(self.marionette)
        except certapp.CloseError:
            self.close_app_manually()

        try:
            self.app = certapp.launch(self.marionette)
            self.showTestStatusInDevice(self.marionette)
        except certapp.LaunchError:
            self.launch_app_manually()
Ejemplo n.º 3
0
def run(
    suite,
    logger,
    spawn_browser=True,
    verbosity=1,
    quiet=False,
    failfast=False,
    catch_break=False,
    buffer=True,
    **kwargs
):
    """A simple test runner.

    This test runner is essentially equivalent to ``unittest.main``
    from the standard library, but adds support for loading test
    classes with extra keyword arguments.

    The easiest way to run a test is via the command line::

        python -m semiauto test_sms

    See the standard library unittest module for ways in which tests
    can be specified.

    For example it is possible to automatically discover tests::

        python -m semiauto discover ."""
    if catch_break:
        import unittest.signals

        unittest.signals.installHandler()

    env = environment.get(
        environment.InProcessTestEnvironment,
        addr=None if spawn_browser else ("127.0.0.1", 6666),
        verbose=(verbosity > 1),
    )

    url = "http://%s:%d/" % (env.server.addr[0], env.server.addr[1])
    if spawn_browser:
        import webbrowser

        webbrowser.open(url)
    else:
        print >>sys.stderr, "Please connect your browser to %s" % url

    # Wait for browser to connect and get socket connection to client
    try:
        so = server.wait_for_client()
    except server.ConnectError as e:
        logger.error("%s: error: %s" % (sys.argv[0], e))
        sys.exit(1)

    tests = serialize_suite(suite)
    test_runner = StructuredTestRunner(logger=logger, test_list=tests)

    # This is a hack to make the test suite metadata and the handler
    # available to the tests.
    so.suite = suite
    environment.env.handler = so

    logger.suite_start(tests=tests)
    try:
        results = test_runner.run(suite)
    except (SystemExit, KeyboardInterrupt) as e:
        sys.exit(1)
    logger.suite_end()

    return results
def main():
    parser = argparse.ArgumentParser(
        description="Runner for guided Web API tests.")
    parser.add_argument("-l", "--list-test-groups", action="store_true",
                        help="List all logical test groups")
    parser.add_argument("-a", "--list-all-tests", action="store_true",
                        help="List all tests")
    parser.add_argument("-i", "--include", metavar="GROUP", action="append", default=[],
                        help="Only include specified group(s) in run, include several "
                        "groups by repeating flag")
    parser.add_argument("-n", "--no-browser", action="store_true",
                        help="Don't start a browser but wait for manual connection")
    parser.add_argument("--version", action="store", dest="version",
                        help="B2G version")
    parser.add_argument('-H', '--host',
                        help='Hostname or ip for target device',
                        action='store', default='localhost')
    parser.add_argument('-P', '--port',
                        help='Port for target device',
                        action='store', default=2828)
    parser.add_argument('-p', "--device-profile", action="store",  type=os.path.abspath,
                        help="specify the device profile file path which could include skipped test case information")
    parser.add_argument(
        "-v", dest="verbose", action="store_true", help="Verbose output")
    commandline.add_logging_group(parser)
    args = parser.parse_args(sys.argv[1:])
    logger = commandline.setup_logging(
        "webapi", vars(args), {"raw": sys.stdout})

    if args.list_test_groups and len(args.include) > 0:
        print >> sys.stderr("%s: error: cannot list and include test "
                            "groups at the same time" % sys.argv[0])
        parser.print_usage()
        sys.exit(1)

    testgen = iter_tests(os.path.dirname(__file__))
    if args.list_test_groups:
        for group, _ in testgen:
            print(group)
        return 0
    elif args.list_all_tests:
        for group, tests in testgen:
            for test in tests:
                print("%s.%s" % (group, test))
        return 0

    semiauto.testcase._host = args.host
    semiauto.testcase._port = int(args.port)

    env = environment.get(environment.InProcessTestEnvironment)
    environment.env.device_profile = None
    if args.device_profile:
        with open(args.device_profile, 'r') as device_profile_file:
            environment.env.device_profile = json.load(device_profile_file)['result']

    test_loader = semiauto.TestLoader(version=args.version)
    tests = test_loader.loadTestsFromNames(
        map(lambda t: "mcts.webapi_tests.%s" % t, args.include or [g for g, _ in testgen]), None)
    results = semiauto.run(tests,
                           logger=logger,
                           spawn_browser=not args.no_browser,
                           verbosity=2 if args.verbose else 1)
    return 0 if results.wasSuccessful() else 1
def run(suite,
        logger,
        spawn_browser=True,
        verbosity=1,
        quiet=False,
        failfast=False,
        catch_break=False,
        buffer=True,
        **kwargs):
    """A simple test runner.

    This test runner is essentially equivalent to ``unittest.main``
    from the standard library, but adds support for loading test
    classes with extra keyword arguments.

    The easiest way to run a test is via the command line::

        python -m semiauto test_sms

    See the standard library unittest module for ways in which tests
    can be specified.

    For example it is possible to automatically discover tests::

        python -m semiauto discover ."""
    if catch_break:
        import unittest.signals
        unittest.signals.installHandler()

    env = environment.get(environment.InProcessTestEnvironment,
                          addr=None if spawn_browser else ("127.0.0.1", 6666),
                          verbose=(verbosity > 1))

    url = "http://%s:%d/" % (env.server.addr[0], env.server.addr[1])
    if spawn_browser:
        import webbrowser
        webbrowser.open(url)
    else:
        print >> sys.stderr, "Please connect your browser to %s" % url

    # Wait for browser to connect and get socket connection to client
    try:
        so = server.wait_for_client()
    except server.ConnectError as e:
        logger.error("%s: error: %s" % (sys.argv[0], e))
        sys.exit(1)

    tests = serialize_suite(suite)
    test_runner = StructuredTestRunner(logger=logger, test_list=tests)

    # This is a hack to make the test suite metadata and the handler
    # available to the tests.
    so.suite = suite
    environment.env.handler = so

    logger.suite_start(tests=tests)
    try:
        results = test_runner.run(suite)
    except (SystemExit, KeyboardInterrupt) as e:
        sys.exit(1)
    logger.suite_end()

    return results
def main():
    parser = argparse.ArgumentParser(
        description="Runner for guided Web API tests.")
    parser.add_argument("-l",
                        "--list-test-groups",
                        action="store_true",
                        help="List all logical test groups")
    parser.add_argument("-a",
                        "--list-all-tests",
                        action="store_true",
                        help="List all tests")
    parser.add_argument(
        "-i",
        "--include",
        metavar="GROUP",
        action="append",
        default=[],
        help="Only include specified group(s) in run, include several "
        "groups by repeating flag")
    parser.add_argument(
        "-n",
        "--no-browser",
        action="store_true",
        help="Don't start a browser but wait for manual connection")
    parser.add_argument("--version",
                        action="store",
                        dest="version",
                        help="B2G version")
    parser.add_argument('-H',
                        '--host',
                        help='Hostname or ip for target device',
                        action='store',
                        default='localhost')
    parser.add_argument('-P',
                        '--port',
                        help='Port for target device',
                        action='store',
                        default=2828)
    parser.add_argument('-m',
                        '--mode',
                        help='Test mode (stingray, phone) default (phone)',
                        action='store',
                        default='phone')
    parser.add_argument(
        '-p',
        "--device-profile",
        action="store",
        type=os.path.abspath,
        help=
        "specify the device profile file path which could include skipped test case information"
    )
    parser.add_argument("-v",
                        dest="verbose",
                        action="store_true",
                        help="Verbose output")
    commandline.add_logging_group(parser)
    args = parser.parse_args(sys.argv[1:])
    logger = commandline.setup_logging("webapi", vars(args),
                                       {"raw": sys.stdout})

    if args.list_test_groups and len(args.include) > 0:
        print >> sys.stderr("%s: error: cannot list and include test "
                            "groups at the same time" % sys.argv[0])
        parser.print_usage()
        sys.exit(1)

    testgen = iter_tests(os.path.dirname(__file__), mode=args.mode)
    if args.list_test_groups:
        for group, _ in testgen:
            print(group)
        return 0
    elif args.list_all_tests:
        for group, tests in testgen:
            for test in tests:
                print("%s.%s" % (group, test))
        return 0

    semiauto.testcase._host = args.host
    semiauto.testcase._port = int(args.port)

    env = environment.get(environment.InProcessTestEnvironment)
    environment.env.device_profile = None
    if args.device_profile:
        with open(args.device_profile, 'r') as device_profile_file:
            environment.env.device_profile = json.load(
                device_profile_file)['result']

    test_loader = semiauto.TestLoader(version=args.version)
    tests = test_loader.loadTestsFromNames(
        map(lambda t: "mcts.webapi_tests.%s" % t, args.include
            or [g for g, _ in testgen]), None)
    results = semiauto.run(tests,
                           logger=logger,
                           spawn_browser=not args.no_browser,
                           verbosity=2 if args.verbose else 1)
    return 0 if results.wasSuccessful() else 1