Skip to content

DB reporting plugin for pytest #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand All @@ -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')
Expand All @@ -63,13 +75,17 @@ 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+')
config_file.write("with_selenium:::%s\n" % with_selenium)
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)
Expand Down
72 changes: 69 additions & 3 deletions seleniumbase/fixtures/base_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@
by giving page elements enough time to load before taking action on them.
"""

import getpass
import json
import logging
import os
import pytest
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
Expand Down Expand Up @@ -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
Expand All @@ -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):
"""
Expand All @@ -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)
Expand All @@ -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)