From a26cdceb4b29b2c69d2240996588f55bc39cebc7 Mon Sep 17 00:00:00 2001 From: Matthias <5024695+xmatthias@users.noreply.github.com> Date: Sat, 7 Apr 2018 20:06:53 +0200 Subject: [PATCH] Fix tests run in random order (#599) * allow tests to run in random mode * Fix random test mode for fiat-convert * allow random test execution in persistence * fix pep8 styling * use "usefixtures" to prevent pylint "unused parameter" message * add pytest-random-order to travis --- .travis.yml | 4 ++-- freqtrade/tests/conftest.py | 2 +- freqtrade/tests/exchange/test_exchange.py | 6 +++--- freqtrade/tests/test_fiat_convert.py | 4 +++- freqtrade/tests/test_persistence.py | 14 ++++++++++++++ 5 files changed, 23 insertions(+), 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index be330dad4..6554f2095 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,7 +13,7 @@ addons: install: - ./install_ta-lib.sh - export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH -- pip install --upgrade flake8 coveralls +- pip install --upgrade flake8 coveralls pytest-random-order - pip install -r requirements.txt - pip install -e . jobs: @@ -34,4 +34,4 @@ notifications: cache: directories: - $HOME/.cache/pip - - ta-lib \ No newline at end of file + - ta-lib diff --git a/freqtrade/tests/conftest.py b/freqtrade/tests/conftest.py index 48bfcaf89..5e35537c9 100644 --- a/freqtrade/tests/conftest.py +++ b/freqtrade/tests/conftest.py @@ -46,7 +46,7 @@ def get_patched_freqtradebot(mocker, config) -> FreqtradeBot: return FreqtradeBot(config, create_engine('sqlite://')) -@pytest.fixture(scope="module") +@pytest.fixture(scope="function") def default_conf(): """ Returns validated configuration suitable for most tests """ configuration = { diff --git a/freqtrade/tests/exchange/test_exchange.py b/freqtrade/tests/exchange/test_exchange.py index f2874f2da..578c904b3 100644 --- a/freqtrade/tests/exchange/test_exchange.py +++ b/freqtrade/tests/exchange/test_exchange.py @@ -16,9 +16,9 @@ from freqtrade.tests.conftest import log_has API_INIT = False -def maybe_init_api(conf, mocker): +def maybe_init_api(conf, mocker, force=False): global API_INIT - if not API_INIT: + if force or not API_INIT: mocker.patch('freqtrade.exchange.validate_pairs', side_effect=lambda s: True) init(config=conf) @@ -27,7 +27,7 @@ def maybe_init_api(conf, mocker): def test_init(default_conf, mocker, caplog): caplog.set_level(logging.INFO) - maybe_init_api(default_conf, mocker) + maybe_init_api(default_conf, mocker, True) assert log_has('Instance is running with dry_run enabled', caplog.record_tuples) diff --git a/freqtrade/tests/test_fiat_convert.py b/freqtrade/tests/test_fiat_convert.py index 49a2adc05..dcbc7b936 100644 --- a/freqtrade/tests/test_fiat_convert.py +++ b/freqtrade/tests/test_fiat_convert.py @@ -126,8 +126,10 @@ def test_fiat_convert_get_price(mocker): def test_fiat_convert_without_network(): # Because CryptoToFiatConverter is a Singleton we reset the value of _coinmarketcap - CryptoToFiatConverter._coinmarketcap = None fiat_convert = CryptoToFiatConverter() + + CryptoToFiatConverter._coinmarketcap = None + assert fiat_convert._coinmarketcap is None assert fiat_convert._find_price(crypto_symbol='BTC', fiat_symbol='USD') == 0.0 diff --git a/freqtrade/tests/test_persistence.py b/freqtrade/tests/test_persistence.py index 70199b12a..e4e16bcd1 100644 --- a/freqtrade/tests/test_persistence.py +++ b/freqtrade/tests/test_persistence.py @@ -8,6 +8,11 @@ from freqtrade.exchange import Exchanges from freqtrade.persistence import Trade, init, clean_dry_run_db +@pytest.fixture(scope='function') +def init_persistence(default_conf): + init(default_conf) + + def test_init_create_session(default_conf, mocker): mocker.patch.dict('freqtrade.persistence._CONF', default_conf) @@ -90,6 +95,7 @@ def test_init_prod_db(default_conf, mocker): os.rename(prod_db_swp, prod_db) +@pytest.mark.usefixtures("init_persistence") def test_update_with_bittrex(limit_buy_order, limit_sell_order): """ On this test we will buy and sell a crypto currency. @@ -144,6 +150,7 @@ def test_update_with_bittrex(limit_buy_order, limit_sell_order): assert trade.close_date is not None +@pytest.mark.usefixtures("init_persistence") def test_calc_open_close_trade_price(limit_buy_order, limit_sell_order): trade = Trade( pair='BTC_ETH', @@ -166,6 +173,7 @@ def test_calc_open_close_trade_price(limit_buy_order, limit_sell_order): assert trade.calc_profit_percent() == 0.06201057 +@pytest.mark.usefixtures("init_persistence") def test_calc_close_trade_price_exception(limit_buy_order): trade = Trade( pair='BTC_ETH', @@ -179,6 +187,7 @@ def test_calc_close_trade_price_exception(limit_buy_order): assert trade.calc_close_trade_price() == 0.0 +@pytest.mark.usefixtures("init_persistence") def test_update_open_order(limit_buy_order): trade = Trade( pair='BTC_ETH', @@ -201,6 +210,7 @@ def test_update_open_order(limit_buy_order): assert trade.close_date is None +@pytest.mark.usefixtures("init_persistence") def test_update_invalid_order(limit_buy_order): trade = Trade( pair='BTC_ETH', @@ -213,6 +223,7 @@ def test_update_invalid_order(limit_buy_order): trade.update(limit_buy_order) +@pytest.mark.usefixtures("init_persistence") def test_calc_open_trade_price(limit_buy_order): trade = Trade( pair='BTC_ETH', @@ -230,6 +241,7 @@ def test_calc_open_trade_price(limit_buy_order): assert trade.calc_open_trade_price(fee=0.003) == 0.001003000 +@pytest.mark.usefixtures("init_persistence") def test_calc_close_trade_price(limit_buy_order, limit_sell_order): trade = Trade( pair='BTC_ETH', @@ -251,6 +263,7 @@ def test_calc_close_trade_price(limit_buy_order, limit_sell_order): assert trade.calc_close_trade_price(fee=0.005) == 0.0010619972 +@pytest.mark.usefixtures("init_persistence") def test_calc_profit(limit_buy_order, limit_sell_order): trade = Trade( pair='BTC_ETH', @@ -281,6 +294,7 @@ def test_calc_profit(limit_buy_order, limit_sell_order): assert trade.calc_profit(fee=0.003) == 0.00006163 +@pytest.mark.usefixtures("init_persistence") def test_calc_profit_percent(limit_buy_order, limit_sell_order): trade = Trade( pair='BTC_ETH',