Skip to content

Commit

Permalink
Fix: Local database always created (#17)
Browse files Browse the repository at this point in the history
* PM #14 - Do not create local database if --no-db option has been set. Specific test written.
  • Loading branch information
js-dieu committed Apr 17, 2020
1 parent 3c61280 commit 34cd11e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
5 changes: 3 additions & 2 deletions pytest_monitor/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

class PyTestMonitorSession(object):
def __init__(self, db=None, remote=None, component='', scope=None):
self.__db = DBHandler(db) if db else None
self.__db = None
if db:
self.__db = DBHandler(db)
self.__remote = remote
self.__component = component
self.__session = ''
Expand Down Expand Up @@ -74,7 +76,6 @@ def compute_info(self, description):
msg = f"Cannot insert session in remote monitor server ({r.status_code})! Deactivating...')"
warnings.warn(msg)


def set_environment_info(self, env):
self.__eid = self.get_env_id(env)
db_id, remote_id = self.__eid
Expand Down
38 changes: 38 additions & 0 deletions tests/test_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,41 @@ def test_monitored():
cursor = db.cursor()
cursor.execute('SELECT ITEM FROM TEST_METRICS;')
assert 1 == len(cursor.fetchall())


def test_monitor_no_db(testdir):
"""Make sure that pytest-monitor correctly understand the monitor_skip_test_if marker."""

# create a temporary pytest test module
testdir.makepyfile("""
import pytest
import time
def test_it():
time.sleep(0.1)
x = ['a' * i for i in range(100)]
assert len(x) == 100
def test_that():
time.sleep(0.1)
x = ['a' *i for i in range(100)]
assert len(x) == 100
""")

wrn = 'pytest-monitor: No storage specified but monitoring is requested. Disabling monitoring.'
with pytest.warns(UserWarning, match=wrn):
# run pytest with the following cmd args
result = testdir.runpytest('--no-db', '-v')

# fnmatch_lines does an assertion internally
result.stdout.fnmatch_lines(['*::test_it PASSED*',
'*::test_that PASSED*'])

pymon_path = pathlib.Path(str(testdir)) / '.pymon'
assert not pymon_path.exists()

# make sure that that we get a '0' exit code for the testsuite
result.assert_outcomes(passed=2)

0 comments on commit 34cd11e

Please sign in to comment.