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
This commit is contained in:
Matthias 2018-04-07 20:06:53 +02:00 committed by gcarq
parent f4077a51c1
commit 23e989d31f
5 changed files with 23 additions and 7 deletions

View File

@ -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
- ta-lib

View File

@ -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 = {

View File

@ -17,9 +17,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)
@ -28,7 +28,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)

View File

@ -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

View File

@ -7,6 +7,11 @@ from sqlalchemy import create_engine
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)
@ -89,6 +94,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, fee):
"""
On this test we will buy and sell a crypto currency.
@ -143,6 +149,7 @@ def test_update_with_bittrex(limit_buy_order, limit_sell_order, fee):
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, fee):
trade = Trade(
pair='ETH/BTC',
@ -165,6 +172,7 @@ def test_calc_open_close_trade_price(limit_buy_order, limit_sell_order, fee):
assert trade.calc_profit_percent() == 0.06201057
@pytest.mark.usefixtures("init_persistence")
def test_calc_close_trade_price_exception(limit_buy_order, fee):
trade = Trade(
pair='ETH/BTC',
@ -178,6 +186,7 @@ def test_calc_close_trade_price_exception(limit_buy_order, fee):
assert trade.calc_close_trade_price() == 0.0
@pytest.mark.usefixtures("init_persistence")
def test_update_open_order(limit_buy_order):
trade = Trade(
pair='ETH/BTC',
@ -200,6 +209,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='ETH/BTC',
@ -212,6 +222,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, fee):
trade = Trade(
pair='ETH/BTC',
@ -229,6 +240,7 @@ def test_calc_open_trade_price(limit_buy_order, fee):
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, fee):
trade = Trade(
pair='ETH/BTC',
@ -250,6 +262,7 @@ def test_calc_close_trade_price(limit_buy_order, limit_sell_order, fee):
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, fee):
trade = Trade(
pair='ETH/BTC',
@ -280,6 +293,7 @@ def test_calc_profit(limit_buy_order, limit_sell_order, fee):
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, fee):
trade = Trade(
pair='ETH/BTC',