Move output-fixture to confest.py and yield path

* Modify test_log and test_template to use these changes
This commit is contained in:
Thor77 2017-04-28 23:24:31 +02:00
parent e37e292ba9
commit 8299c73eb9
3 changed files with 19 additions and 18 deletions

13
tsstats/tests/conftest.py Normal file
View File

@ -0,0 +1,13 @@
from os import remove
import pytest
@pytest.fixture
def output(request):
output_path = 'tsstats/tests/res/output.html'
def clean():
remove(output_path)
request.addfinalizer(clean)
yield output_path

View File

@ -1,5 +1,4 @@
from datetime import datetime, timedelta from datetime import datetime, timedelta
from os import remove
from time import sleep from time import sleep
import pytest import pytest
@ -7,7 +6,6 @@ import pytest
from tsstats.exceptions import InvalidLog from tsstats.exceptions import InvalidLog
from tsstats.log import TimedLog, _bundle_logs, _parse_details, parse_logs from tsstats.log import TimedLog, _bundle_logs, _parse_details, parse_logs
from tsstats.template import render_servers from tsstats.template import render_servers
from tsstats.tests.test_template import output_path
testlog_path = 'tsstats/tests/res/test.log' testlog_path = 'tsstats/tests/res/test.log'
@ -92,10 +90,9 @@ def test_parse_groups():
assert len(clients) == 0 assert len(clients) == 0
def test_parse_utf8(): def test_parse_utf8(output):
servers = parse_logs(testlog_path + '.utf8') servers = parse_logs(testlog_path + '.utf8')
render_servers(servers, output_path) render_servers(servers, output)
remove(output_path)
def test_server_stop(): def test_server_stop():

View File

@ -1,6 +1,5 @@
import logging import logging
from datetime import timedelta from datetime import timedelta
from os import remove
import pytest import pytest
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
@ -9,31 +8,23 @@ from tsstats.log import Server, _parse_details
from tsstats.template import render_servers from tsstats.template import render_servers
from tsstats.utils import filter_threshold, seconds_to_text, sort_clients from tsstats.utils import filter_threshold, seconds_to_text, sort_clients
output_path = 'tsstats/tests/res/output.html'
clients = _parse_details('tsstats/tests/res/test.log', online_dc=False) clients = _parse_details('tsstats/tests/res/test.log', online_dc=False)
servers = [Server(1, clients)] servers = [Server(1, clients)]
logger = logging.getLogger('tsstats') logger = logging.getLogger('tsstats')
@pytest.fixture
def output(request):
def clean():
remove(output_path)
request.addfinalizer(clean)
@pytest.fixture @pytest.fixture
def soup(output): def soup(output):
render_servers(servers, output_path) render_servers(servers, output)
return BeautifulSoup(open(output_path), 'html.parser') return BeautifulSoup(open(output), 'html.parser')
def test_debug(output): def test_debug(output):
logger.setLevel(logging.DEBUG) logger.setLevel(logging.DEBUG)
render_servers(servers, output_path) render_servers(servers, output)
logger.setLevel(logging.INFO) logger.setLevel(logging.INFO)
soup = BeautifulSoup(open(output_path), 'html.parser') soup = BeautifulSoup(open(output), 'html.parser')
# check debug-label presence # check debug-label presence
assert soup.find_all(style='color: red; padding-right: 10px;') assert soup.find_all(style='color: red; padding-right: 10px;')
for client_item in soup.find('ul', id='1.onlinetime').find_all('li'): for client_item in soup.find('ul', id='1.onlinetime').find_all('li'):