Skip to content

Commit

Permalink
some better pytest practices, test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lmtierney committed Sep 18, 2018
1 parent f637b81 commit c0bc0f0
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 45 deletions.
7 changes: 7 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ def quick_timeout():
nerodia.default_timeout = orig_timeout


@pytest.fixture
def timeout_reset():
original = nerodia.default_timeout
yield
nerodia.default_timeout = original


@pytest.fixture(autouse=True)
def start_page(request, page):
marker = request.node.get_closest_marker('page')
Expand Down
4 changes: 1 addition & 3 deletions tests/browser/alert_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,12 @@ def test_waits_until_alert_is_present_and_goes_on(self, browser):

@pytest.mark.skipif('nerodia.relaxed_locate',
reason='only applicable when not relaxed locating')
@pytest.mark.usefixtures('timeout_reset')
def test_raises_error_if_alert_is_not_present_after_timeout(self, browser):
orig_timeout = nerodia.default_timeout
nerodia.default_timeout = 2
with pytest.raises(TimeoutError):
browser.alert.wait_until_present().ok()

nerodia.default_timeout = orig_timeout

# confirm

def test_accepts_confirm(self, browser):
Expand Down
15 changes: 10 additions & 5 deletions tests/browser/drag_and_drop_tests.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import pytest


@pytest.fixture()
@pytest.fixture
def draggable(browser):
yield browser.div(id='draggable')


@pytest.fixture()
@pytest.fixture
def droppable(browser):
yield browser.div(id='droppable')

Expand Down Expand Up @@ -38,11 +38,16 @@ def test_can_drag_and_drop_an_element_onto_another_when_draggable_is_out_of_view
reposition(browser, 'draggable')
perform_drag_and_drop_on_droppable(draggable, droppable)

@pytest.mark.xfail_firefox(reason='flaky test on Travis')
@pytest.mark.xfail_firefox
@pytest.mark.xfail_ie
@pytest.mark.quits_browser
def test_can_drag_and_drop_an_element_onto_another_when_droppable_is_out_of_viewport(self, browser, draggable, droppable):
reposition(browser, 'droppable')
perform_drag_and_drop_on_droppable(draggable, droppable)

@pytest.mark.quits_browser
def test_can_drag_an_element_by_the_given_offset(self, browser, draggable, droppable):
assert droppable.text == 'Drop here'
y = 150 if browser.wd.w3c else 50
draggable.drag_and_drop_by(200, y)
draggable.drag_and_drop_by(200, 50)
droppable.wait_until(lambda e: e.text == 'Dropped!', timeout=2)
assert droppable.text == 'Dropped!'
13 changes: 5 additions & 8 deletions tests/browser/elements/select_list_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,20 +317,17 @@ def test_selects_options_with_a_single_quoted_value(self, browser):

@pytest.mark.skipif('nerodia.relaxed_locate is False',
reason='only applicable when relaxed locating')
@pytest.mark.usefixtures('timeout_reset')
@pytest.mark.page('wait.html')
def test_waits_to_select_an_option(self, browser):
from time import time
start_timeout = nerodia.default_timeout
browser.link(id='add_select').click()
select_list = browser.select_list(id='languages')
nerodia.default_timeout = 2
try:
start_time = time()
with pytest.raises(NoValueFoundException):
select_list.select('No')
assert time() - start_time > 2
finally:
nerodia.default_timeout = start_timeout
start_time = time()
with pytest.raises(NoValueFoundException):
select_list.select('No')
assert time() - start_time > 2

@pytest.mark.usefixtures('quick_timeout')
def test_raises_correct_exception_if_the_option_doesnt_exist(self, browser):
Expand Down
7 changes: 0 additions & 7 deletions tests/browser/relaxed_locate_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@
from nerodia.exception import UnknownObjectException


@pytest.fixture
def timeout_reset():
original = nerodia.default_timeout
yield
nerodia.default_timeout = original


@pytest.mark.page('wait.html')
@pytest.mark.usefixtures('timeout_reset')
@pytest.mark.skipif('nerodia.relaxed_locate is False',
Expand Down
19 changes: 9 additions & 10 deletions tests/element_locator_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,19 +303,19 @@ def test_returns_none_if_found_element_didnt_match_the_selector_tag_name(self, b

# errors

def test_raises_correct_exception_if_index_is_not_an_integer(self, browser, mocker, expect_all):
with pytest.raises(TypeError) as e:
def test_raises_correct_exception_if_index_is_not_an_integer(self, browser, expect_all):
message = "expected {}, got 'bar':{}".format(int, str)
with pytest.raises(TypeError, message=message):
selector = {'tag_name': 'div', 'index': 'bar'}
locate_one(browser, selector)
assert e.value.args[0] == "expected {}, got 'bar':{}".format(int, str)

def test_raises_correct_exception_if_selector_value_is_not_a_list_string_unicode_regexp_or_boolean(self, browser, mocker, expect_all):
def test_raises_correct_exception_if_selector_value_is_not_a_list_string_unicode_regexp_or_boolean(self, browser, expect_all):
from nerodia.locators.element.selector_builder import SelectorBuilder
with pytest.raises(TypeError) as e:
expected = SelectorBuilder.VALID_WHATS + [int]
message = "expected one of [{}, {}, {}, {}, {}], got 123:{}".format(*expected)
with pytest.raises(TypeError, message=message):
selector = {'tag_name': 123}
locate_one(browser, selector)
expected = SelectorBuilder.VALID_WHATS + [int]
assert e.value.args[0] == "expected one of [{}, {}, {}, {}, {}], got 123:{}".format(*expected)


class TestElementLocatorFindsSeveralElements(object):
Expand Down Expand Up @@ -423,8 +423,7 @@ def test_does_not_try_to_convert_expressions_containing_pipe(self, browser, mock

# errors

def test_raises_correct_exception_if_index_is_given(self, browser, mocker, expect_all):
with pytest.raises(ValueError) as e:
def test_raises_correct_exception_if_index_is_given(self, browser, expect_all):
with pytest.raises(ValueError, message="can't locate all elements by index"):
selector = {'tag_name': 'div', 'index': 1}
locate_all(browser, selector)
assert e.value.args[0] == "can't locate all elements by index"
16 changes: 4 additions & 12 deletions tests/unit/logger_tests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import logging
import os
import tempfile

import pytest

Expand All @@ -15,14 +13,6 @@ def default_logging_handling():
nerodia.logger.filename = None


@pytest.fixture
def filepath():
filepath = os.path.join(tempfile.gettempdir(), 'log.tmp')
yield filepath
if os.path.isfile(filepath):
os.remove(filepath)


def test_logs_warnings_by_default():
assert nerodia.logger.level == logging.WARNING

Expand Down Expand Up @@ -51,7 +41,8 @@ def test_outputs_to_stdout_by_default(caplog):
assert 'warning_message' in caplog.text


def test_allows_to_output_to_file(filepath):
def test_allows_to_output_to_file(temp_file):
filepath = temp_file.name
nerodia.logger.filename = filepath
nerodia.logger.warning('warning_message1')
with open(filepath) as f:
Expand All @@ -65,7 +56,8 @@ def test_allows_to_output_to_file(filepath):
assert 'warning_message2' in text


def test_allows_stopping_output_to_file(filepath):
def test_allows_stopping_output_to_file(temp_file):
filepath = temp_file.name
nerodia.logger.filename = filepath
nerodia.logger.warning('warning_message1')
with open(filepath) as f:
Expand Down

0 comments on commit c0bc0f0

Please sign in to comment.