From 8435f7f6fd29df57d7ad319a1e90d4123dc17a55 Mon Sep 17 00:00:00 2001 From: Michael Mintz Date: Sun, 14 Feb 2016 17:25:15 -0500 Subject: [PATCH 1/2] Add pytest DB reporting to conftest --- conftest.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/conftest.py b/conftest.py index 00c8d3bb63f..88ee2d4adcd 100755 --- a/conftest.py +++ b/conftest.py @@ -3,6 +3,7 @@ import os import shutil import time +from optparse import SUPPRESS_HELP from seleniumbase.config import settings from seleniumbase.fixtures import constants @@ -34,6 +35,15 @@ def pytest_addoption(parser): parser.addoption('--log_path', dest='log_path', default='logs/', help='Where the log files are saved.') + parser.addoption('--with-db_reporting', action="store_true", + dest='with_db_reporting', + default=False, + help="Use to record test data in the MySQL database.") + parser.addoption('--database_env', action='store', + dest='database_env', + choices=('prod', 'qa', 'test'), + default='test', + help=SUPPRESS_HELP) parser.addoption('--headless', action="store_true", dest='headless', default=False, @@ -53,6 +63,8 @@ def pytest_addoption(parser): def pytest_configure(config): with_selenium = config.getoption('with_selenium') with_testing_base = config.getoption('with_testing_base') + with_db_reporting = config.getoption('with_db_reporting') + database_env = config.getoption('database_env') browser = config.getoption('browser') log_path = config.getoption('log_path') headless = config.getoption('headless') @@ -63,6 +75,8 @@ def pytest_configure(config): demo_sleep = config.getoption('demo_sleep') if config.getoption('data') is not None: data = config.getoption('data') + if config.getoption('database_env') is not None: + database_env = config.getoption('database_env') # Create a temporary config file while tests are running pytest_config = '.pytest_config' config_file = open(pytest_config, 'w+') @@ -70,6 +84,8 @@ def pytest_configure(config): config_file.write("browser:::%s\n" % browser) config_file.write("data:::%s\n" % data) config_file.write("with_testing_base:::%s\n" % with_testing_base) + config_file.write("with_db_reporting:::%s\n" % with_db_reporting) + config_file.write("database_env:::%s\n" % database_env) config_file.write("log_path:::%s\n" % log_path) config_file.write("headless:::%s\n" % headless) config_file.write("demo_mode:::%s\n" % demo_mode) From d21973781c1641a082f0f63aa5eab76233540297 Mon Sep 17 00:00:00 2001 From: Michael Mintz Date: Sun, 14 Feb 2016 17:26:14 -0500 Subject: [PATCH 2/2] Add pytest DB reporting to base_case --- seleniumbase/fixtures/base_case.py | 72 ++++++++++++++++++++++++++++-- 1 file changed, 69 insertions(+), 3 deletions(-) diff --git a/seleniumbase/fixtures/base_case.py b/seleniumbase/fixtures/base_case.py index cc6feb51222..4f5698c8418 100755 --- a/seleniumbase/fixtures/base_case.py +++ b/seleniumbase/fixtures/base_case.py @@ -4,6 +4,7 @@ by giving page elements enough time to load before taking action on them. """ +import getpass import json import logging import os @@ -11,10 +12,16 @@ import sys import time import unittest +import uuid from pyvirtualdisplay import Display from seleniumbase.config import settings +from seleniumbase.core.application_manager import ApplicationManager +from seleniumbase.core.testcase_manager import ExecutionQueryPayload +from seleniumbase.core.testcase_manager import TestcaseDataPayload +from seleniumbase.core.testcase_manager import TestcaseManager from seleniumbase.core import browser_launcher from seleniumbase.core import log_helper +from seleniumbase.fixtures import constants from selenium.webdriver.remote.webdriver import WebDriver from selenium.webdriver.common.by import By import page_actions @@ -325,10 +332,15 @@ def setUp(self): # Not using pytest (probably nosetests) self.is_pytest = False if self.is_pytest: + test_id = "%s.%s.%s" % (self.__class__.__module__, + self.__class__.__name__, + self._testMethodName) self.with_selenium = pytest.config.option.with_selenium self.headless = pytest.config.option.headless self.headless_active = False self.with_testing_base = pytest.config.option.with_testing_base + self.with_db_reporting = pytest.config.option.with_db_reporting + self.database_env = pytest.config.option.database_env self.log_path = pytest.config.option.log_path self.browser = pytest.config.option.browser self.data = pytest.config.option.data @@ -340,6 +352,52 @@ def setUp(self): self.headless_active = True if self.with_selenium: self.driver = browser_launcher.get_driver(self.browser) + if self.with_db_reporting: + self.execution_guid = str(uuid.uuid4()) + self.testcase_guid = None + self.execution_start_time = 0 + self.case_start_time = 0 + self.application = None + self.testcase_manager = None + self.error_handled = False + self.testcase_manager = TestcaseManager(self.database_env) + # + exec_payload = ExecutionQueryPayload() + exec_payload.execution_start_time = int(time.time() * 1000) + self.execution_start_time = exec_payload.execution_start_time + exec_payload.guid = self.execution_guid + exec_payload.username = getpass.getuser() + self.testcase_manager.insert_execution_data(exec_payload) + # + data_payload = TestcaseDataPayload() + self.testcase_guid = str(uuid.uuid4()) + data_payload.guid = self.testcase_guid + data_payload.execution_guid = self.execution_guid + if self.with_selenium: + data_payload.browser = self.browser + else: + data_payload.browser = "N/A" + data_payload.testcaseAddress = test_id + application = ApplicationManager.generate_application_string( + self._testMethodName) + data_payload.env = application.split('.')[0] + data_payload.start_time = application.split('.')[1] + data_payload.state = constants.State.NOTRUN + self.testcase_manager.insert_testcase_data(data_payload) + self.case_start_time = int(time.time() * 1000) + + def __insert_test_result(self, state, err=None): + data_payload = TestcaseDataPayload() + data_payload.runtime = int(time.time() * 1000) - self.case_start_time + data_payload.guid = self.testcase_guid + data_payload.execution_guid = self.execution_guid + data_payload.state = state + if err is not None: + data_payload.message = err[1].__str__().split( + '''-------------------- >> ''' + '''begin captured logging''' + ''' << --------------------''', 1)[0] + self.testcase_manager.update_testcase_data(data_payload) def tearDown(self): """ @@ -349,12 +407,12 @@ def tearDown(self): super(SubClassOfBaseCase, self).tearDown() """ if self.is_pytest: + test_id = "%s.%s.%s" % (self.__class__.__module__, + self.__class__.__name__, + self._testMethodName) if self.with_selenium: # Save a screenshot if logging is on when an exception occurs if self.with_testing_base and (sys.exc_info()[1] is not None): - test_id = "%s.%s.%s" % (self.__class__.__module__, - self.__class__.__name__, - self._testMethodName) test_logpath = self.log_path + "/" + test_id if not os.path.exists(test_logpath): os.makedirs(test_logpath) @@ -370,3 +428,11 @@ def tearDown(self): if self.headless: if self.headless_active: self.display.stop() + if self.with_db_reporting: + if sys.exc_info()[1] is not None: + self.__insert_test_result(constants.State.ERROR) + else: + self.__insert_test_result(constants.State.PASS) + runtime = int(time.time() * 1000) - self.execution_start_time + self.testcase_manager.update_execution_data( + self.execution_guid, runtime)