Skip to content

Commit

Permalink
test: add an --offline option to pytest
Browse files Browse the repository at this point in the history
Usage:

    $ py.test --offline

This option skips all tests that cannot be run offline, as they are
marked as "online". Either from manual tag:

    import requests

    @pytest.mark.online
    def test_call_google():
        requests.get('https://google.com')

Or from a command example:

    @example('.title http://google.com', '[ Google ] - google.com', online=True)
    def title_command(bot, trigger):
        # ...

This is especially useful for people like me who are always traveling
and can't have access to a reliable Internet connection. This could be
used by Travis to speed-up the test suite, and/or to make it more
reliable.

Existing online examples have been marked as such.
  • Loading branch information
Exirel committed May 18, 2019
1 parent 165d297 commit e94efb5
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 12 deletions.
17 changes: 17 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
import pytest

# This file lists files which should be ignored by pytest
collect_ignore = ["setup.py", "sopel.py", "sopel/modules/ipython.py"]


def pytest_addoption(parser):
parser.addoption('--offline', action='store_true', default=False)


def pytest_collection_modifyitems(config, items):
if not config.getoption('--offline'):
# nothing to skip
return

skip_online = pytest.mark.skip(reason='deactivated when --offline is used')
for item in items:
if 'online' in item.keywords:
item.add_marker(skip_online)
11 changes: 10 additions & 1 deletion sopel/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,9 +549,12 @@ class example(object):
user_help:
Whether this example should be displayed in user-facing help output
such as `.help command`.
online:
If true, pytest will mark it as "online".
"""
def __init__(self, msg, result=None, privmsg=False, admin=False,
owner=False, repeat=1, re=False, ignore=None, user_help=False):
owner=False, repeat=1, re=False, ignore=None,
user_help=False, online=True):
# Wrap result into a list for get_example_test
if isinstance(result, list):
self.result = result
Expand All @@ -565,6 +568,7 @@ def __init__(self, msg, result=None, privmsg=False, admin=False,
self.admin = admin
self.owner = owner
self.repeat = repeat
self.online = online

if isinstance(ignore, list):
self.ignore = ignore
Expand All @@ -580,6 +584,7 @@ def __call__(self, func):
func.example = []

import sys
import pytest

import sopel.test_tools # TODO: fix circular import with sopel.bot and sopel.test_tools

Expand All @@ -590,6 +595,10 @@ def __call__(self, func):
func, self.msg, self.result, self.privmsg, self.admin,
self.owner, self.repeat, self.use_re, self.ignore
)

if self.online:
test = pytest.mark.online(test)

sopel.test_tools.insert_into_module(
test, func.__module__, func.__name__, 'test_example'
)
Expand Down
2 changes: 1 addition & 1 deletion sopel/modules/calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def c(bot, trigger):


@commands('py')
@example('.py len([1,2,3])', '3')
@example('.py len([1,2,3])', '3', online=True)
def py(bot, trigger):
"""Evaluate a Python expression."""
if not trigger.group(2):
Expand Down
3 changes: 2 additions & 1 deletion sopel/modules/ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ def _find_geoip_db(bot):
@example('.ip 8.8.8.8',
r'\[IP\/Host Lookup\] Hostname: \S*dns\S*\.google\S* \| Location: United States \| ISP: AS15169 Google LLC',
re=True,
ignore='Downloading GeoIP database, please wait...')
ignore='Downloading GeoIP database, please wait...',
online=True)
def ip(bot, trigger):
"""IP Lookup tool"""
# Check if there is input at all
Expand Down
17 changes: 12 additions & 5 deletions sopel/modules/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,16 @@ def duck_api(query):

@commands('duck', 'ddg', 'g')
# test for bad Unicode handling in py2
@example('.duck grandorder.wiki chulainn alter', 'https://grandorder.wiki/Cú_Chulainn_(Alter)')
@example(
'.duck grandorder.wiki chulainn alter',
'https://grandorder.wiki/Cú_Chulainn_(Alter)',
online=True)
# the last example (in source line order) is what .help displays
@example('.duck sopel irc bot', r'https?:\/\/sopel\.chat\/?', re=True)
@example(
'.duck sopel irc bot',
r'https?:\/\/sopel\.chat\/?',
re=True,
online=True)
def duck(bot, trigger):
"""Queries DuckDuckGo for the specified input."""
query = trigger.group(2)
Expand Down Expand Up @@ -167,9 +174,9 @@ def search(bot, trigger):


@commands('suggest')
@example('.suggest wikip', 'wikipedia')
@example('.suggest ', 'No query term.')
@example('.suggest lkashdfiauwgeaef', 'Sorry, no result.')
@example('.suggest wikip', 'wikipedia', online=True)
@example('.suggest ', 'No query term.', online=True)
@example('.suggest lkashdfiauwgeaef', 'Sorry, no result.', online=True)
def suggest(bot, trigger):
"""Suggests terms starting with given input"""
if not trigger.group(2):
Expand Down
10 changes: 7 additions & 3 deletions sopel/modules/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,13 @@ def tr(bot, trigger):


@commands('translate', 'tr')
@example('.tr :en :fr my dog', '"mon chien" (en to fr, translate.google.com)')
@example('.tr היי', '"Hey" (iw to en, translate.google.com)')
@example('.tr mon chien', '"my dog" (fr to en, translate.google.com)')
@example('.tr :en :fr my dog',
'"mon chien" (en to fr, translate.google.com)',
online=True)
@example('.tr היי', '"Hey" (iw to en, translate.google.com)', online=True)
@example('.tr mon chien',
'"my dog" (fr to en, translate.google.com)',
online=True)
def tr2(bot, trigger):
"""Translates a phrase, with an optional language hint."""
command = trigger.group(2)
Expand Down
5 changes: 4 additions & 1 deletion sopel/modules/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ def setup(bot):


@module.commands('title')
@module.example('.title https://www.google.com', '[ Google ] - www.google.com')
@module.example(
'.title https://www.google.com',
'[ Google ] - www.google.com',
online=True)
def title_command(bot, trigger):
"""
Show the title or URL information for the given URL, or the last URL seen
Expand Down

0 comments on commit e94efb5

Please sign in to comment.