From 78ad4b4e4b7fab548fd7c0201c6ec7df08997905 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Mon, 23 Sep 2019 13:54:42 +0200 Subject: [PATCH] [IMP] runbot_merge, forwardport: consolidate conftests Converge the pytest setups of runbot_merge and forwardport a bit more (the goal is obviously to eventually share the infrastructure so they run the same way). --- conftest.py | 34 +++++++++++++++++++++++++++++++++ forwardport/tests/conftest.py | 35 ---------------------------------- runbot_merge/tests/conftest.py | 9 ++++++--- runbot_merge/tests/local.py | 4 ++++ runbot_merge/tests/remote.py | 4 ---- 5 files changed, 44 insertions(+), 42 deletions(-) diff --git a/conftest.py b/conftest.py index ac3628aa..d6db1698 100644 --- a/conftest.py +++ b/conftest.py @@ -3,6 +3,7 @@ import configparser import re import subprocess import time +import uuid import psutil import pytest @@ -13,6 +14,10 @@ NGROK_CLI = [ ] def pytest_addoption(parser): + parser.addoption('--addons-path') + parser.addoption('--db', help="DB to run the tests against", default=str(uuid.uuid4())) + parser.addoption("--no-delete", action="store_true", help="Don't delete repo after a failed run") + parser.addoption( '--tunnel', action="store", type="choice", choices=['ngrok', 'localtunnel'], default='ngrok', help="Which tunneling method to use to expose the local Odoo server " @@ -23,6 +28,9 @@ def pytest_addoption(parser): "blow through the former); localtunnel has no rate-limiting but " "the servers are way less reliable") +def pytest_report_header(config): + return 'Running against database ' + config.getoption('--db') + @pytest.fixture(scope="session") def config(pytestconfig): """ Flat version of the pytest config file (pytest.ini), parses to a @@ -140,3 +148,29 @@ def tunnel(pytestconfig, port): p.wait(30) else: raise ValueError("Unsupported %s tunnel method" % tunnel) + +@pytest.fixture(scope='session') +def dbcache(request, module): + """ Creates template DB once per run, then just duplicates it before + starting odoo and running the testcase + """ + db = request.config.getoption('--db') + subprocess.run([ + 'odoo', '--no-http', + '--addons-path', request.config.getoption('--addons-path'), + '-d', db, '-i', module, + '--max-cron-threads', '0', + '--stop-after-init' + ], check=True) + yield db + subprocess.run(['dropdb', db]) + +@pytest.fixture +def db(request, dbcache): + rundb = str(uuid.uuid4()) + subprocess.run(['createdb', '-T', dbcache, rundb], check=True) + + yield rundb + + if not request.config.getoption('--no-delete'): + subprocess.run(['dropdb', rundb], check=True) diff --git a/forwardport/tests/conftest.py b/forwardport/tests/conftest.py index fdb6659b..a48760eb 100644 --- a/forwardport/tests/conftest.py +++ b/forwardport/tests/conftest.py @@ -29,15 +29,6 @@ DEFAULT_CRONS = [ 'runbot_merge.feedback_cron', ] - -def pytest_report_header(config): - return 'Running against database ' + config.getoption('--db') - -def pytest_addoption(parser): - parser.addoption('--db', help="DB to run the tests against", default=str(uuid.uuid4())) - parser.addoption('--addons-path') - parser.addoption("--no-delete", action="store_true", help="Don't delete repo after a failed run") - def wait_for_hook(n=1): time.sleep(10 * n) @@ -137,32 +128,6 @@ def port(): s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) return s.getsockname()[1] -@pytest.fixture(scope='session') -def dbcache(request, module): - """ Creates template DB once per run, then just duplicates it before - starting odoo and running the testcase - """ - db = request.config.getoption('--db') - subprocess.run([ - 'odoo', '--no-http', - '--addons-path', request.config.getoption('--addons-path'), - '-d', db, '-i', module, - '--max-cron-threads', '0', - '--stop-after-init' - ], check=True) - yield db - subprocess.run(['dropdb', db]) - -@pytest.fixture -def db(request, dbcache): - rundb = str(uuid.uuid4()) - subprocess.run(['createdb', '-T', dbcache, rundb], check=True) - - yield rundb - - if not request.config.getoption('--no-delete'): - subprocess.run(['dropdb', rundb], check=True) - @pytest.fixture def server(request, db, port, module): p = subprocess.Popen([ diff --git a/runbot_merge/tests/conftest.py b/runbot_merge/tests/conftest.py index feee06cc..b36d92ea 100644 --- a/runbot_merge/tests/conftest.py +++ b/runbot_merge/tests/conftest.py @@ -1,5 +1,8 @@ +import uuid +import pytest + pytest_plugins = ["local"] -def pytest_addoption(parser): - parser.addoption("--db", action="store", help="Odoo DB to run tests with") - parser.addoption('--addons-path', action='store', help="Odoo's addons path") +@pytest.fixture(scope='session') +def module(): + return 'runbot_merge' diff --git a/runbot_merge/tests/local.py b/runbot_merge/tests/local.py index 6e215da9..4bd88b52 100644 --- a/runbot_merge/tests/local.py +++ b/runbot_merge/tests/local.py @@ -18,6 +18,10 @@ def gh(): with fake_github.Github() as gh: yield gh +@pytest.fixture +def db(dbcache): + return dbcache + @pytest.fixture(scope='session') def registry(request): """ Set up Odoo & yields a registry to the specified db diff --git a/runbot_merge/tests/remote.py b/runbot_merge/tests/remote.py index f5ea4f73..c137debd 100644 --- a/runbot_merge/tests/remote.py +++ b/runbot_merge/tests/remote.py @@ -63,10 +63,6 @@ import requests def pytest_addhooks(pluginmanager): pluginmanager.set_blocked('local') -def pytest_addoption(parser): - parser.addoption("--no-delete", action="store_true", help="Don't delete repo after a failed run") - - PORT=8069 @pytest.fixture(scope='session')