pytest: remove deprecated raises and raises_msg assert functions

This commit is contained in:
shimizukawa 2017-01-06 00:46:42 +09:00
parent f566f003f3
commit f962ad67d2
9 changed files with 115 additions and 66 deletions

View File

@ -16,18 +16,21 @@ from sphinx.application import ExtensionError
from sphinx.domains import Domain
from util import with_app, raises_msg, strip_escseq
import pytest
@with_app()
def test_events(app, status, warning):
def empty():
pass
raises_msg(ExtensionError, "Unknown event name: invalid",
app.connect, "invalid", empty)
with pytest.raises(ExtensionError) as excinfo:
app.connect("invalid", empty)
assert "Unknown event name: invalid" in str(excinfo.value)
app.add_event("my_event")
raises_msg(ExtensionError, "Event 'my_event' already present",
app.add_event, "my_event")
with pytest.raises(ExtensionError) as excinfo:
app.add_event("my_event")
assert "Event 'my_event' already present" in str(excinfo.value)
def mock_callback(a_app, *args):
assert a_app is app
@ -109,12 +112,15 @@ def test_domain_override(app, status, warning):
name = 'foo'
# No domain know named foo.
raises_msg(ExtensionError, 'domain foo not yet registered',
app.override_domain, A)
with pytest.raises(ExtensionError) as excinfo:
app.override_domain(A)
assert 'domain foo not yet registered' in str(excinfo.value)
assert app.add_domain(A) is None
assert app.override_domain(B) is None
raises_msg(ExtensionError, 'new domain not a subclass of registered '
'foo domain', app.override_domain, C)
with pytest.raises(ExtensionError) as excinfo:
app.override_domain(C)
assert 'new domain not a subclass of registered foo domain' in str(excinfo.value)
@with_app(testroot='add_source_parser')

View File

@ -10,10 +10,11 @@
:license: BSD, see LICENSE for details.
"""
from six import PY3, iteritems
import pytest
import mock
from util import TestApp, with_app, gen_with_app, \
raises, raises_msg, assert_in, assert_not_in
assert_in, assert_not_in
import sphinx
from sphinx.config import Config
@ -55,11 +56,14 @@ def test_core_config(app, status, warning):
assert 'nonexisting_value' not in cfg
# invalid values
raises(AttributeError, getattr, cfg, '_value')
raises(AttributeError, getattr, cfg, 'nonexisting_value')
with pytest.raises(AttributeError):
getattr(cfg, '_value')
with pytest.raises(AttributeError):
getattr(cfg, 'nonexisting_value')
# non-value attributes are deleted from the namespace
raises(AttributeError, getattr, cfg, 'sys')
with pytest.raises(AttributeError):
getattr(cfg, 'sys')
# setting attributes
cfg.project = 'Foo'
@ -80,16 +84,20 @@ def test_extension_values(app, status, warning):
assert cfg.value_from_conf_py == 84
# no duplicate values allowed
raises_msg(ExtensionError, 'already present', app.add_config_value,
'html_title', 'x', True)
raises_msg(ExtensionError, 'already present', app.add_config_value,
'value_from_ext', 'x', True)
with pytest.raises(ExtensionError) as excinfo:
app.add_config_value('html_title', 'x', True)
assert 'already present' in str(excinfo.value)
with pytest.raises(ExtensionError) as excinfo:
app.add_config_value('value_from_ext', 'x', True)
assert 'already present' in str(excinfo.value)
def test_errors_warnings(tempdir):
# test the error for syntax errors in the config file
(tempdir / 'conf.py').write_text(u'project = \n', encoding='ascii')
raises_msg(ConfigError, 'conf.py', Config, tempdir, 'conf.py', {}, None)
with pytest.raises(ConfigError) as excinfo:
Config(tempdir, 'conf.py', {}, None)
assert 'conf.py' in str(excinfo.value)
# test the automatic conversion of 2.x only code in configs
(tempdir / 'conf.py').write_text(
@ -119,7 +127,9 @@ def test_errors_warnings(tempdir):
def test_errors_if_setup_is_not_callable(tempdir):
# test the error to call setup() in the config file
(tempdir / 'conf.py').write_text(u'setup = 1')
raises_msg(ConfigError, 'callable', TestApp, srcdir=tempdir)
with pytest.raises(ConfigError) as excinfo:
TestApp(srcdir=tempdir)
assert 'callable' in str(excinfo.value)
@mock.patch.object(sphinx, '__display_version__', '1.3.4')
@ -129,24 +139,24 @@ def test_needs_sphinx():
app.cleanup()
app = TestApp(confoverrides={'needs_sphinx': '1.3.4'}) # OK: equals
app.cleanup()
raises(VersionRequirementError, TestApp,
confoverrides={'needs_sphinx': '1.3.5'}) # NG: greater
with pytest.raises(VersionRequirementError):
TestApp(confoverrides={'needs_sphinx': '1.3.5'}) # NG: greater
# minor version
app = TestApp(confoverrides={'needs_sphinx': '1.2'}) # OK: less
app.cleanup()
app = TestApp(confoverrides={'needs_sphinx': '1.3'}) # OK: equals
app.cleanup()
raises(VersionRequirementError, TestApp,
confoverrides={'needs_sphinx': '1.4'}) # NG: greater
with pytest.raises(VersionRequirementError):
TestApp(confoverrides={'needs_sphinx': '1.4'}) # NG: greater
# major version
app = TestApp(confoverrides={'needs_sphinx': '0'}) # OK: less
app.cleanup()
app = TestApp(confoverrides={'needs_sphinx': '1'}) # OK: equals
app.cleanup()
raises(VersionRequirementError, TestApp,
confoverrides={'needs_sphinx': '2'}) # NG: greater
with pytest.raises(VersionRequirementError):
TestApp(confoverrides={'needs_sphinx': '2'}) # NG: greater
def test_config_eol(tempdir):

View File

@ -12,8 +12,9 @@
import re
from six import text_type
import pytest
from util import raises, with_app
from util import with_app
from sphinx import addnodes
from sphinx.domains.cpp import DefinitionParser, DefinitionError, NoOldIdError
@ -150,9 +151,10 @@ def test_concept_definitions():
None, 'I0EN1A1B7ConceptE')
check('concept', 'template<typename A, typename B, typename ...C> Foo()',
None, 'I00DpE3Foo')
raises(DefinitionError, parse, 'concept', 'Foo')
raises(DefinitionError, parse, 'concept',
'template<typename T> template<typename U> Foo')
with pytest.raises(DefinitionError):
parse('concept', 'Foo')
with pytest.raises(DefinitionError):
parse('concept', 'template<typename T> template<typename U> Foo')
def test_member_definitions():
@ -259,9 +261,12 @@ def test_function_definitions():
'int foo(Foo f = Foo(double(), std::make_pair(int(2), double(3.4))))',
"foo__Foo", "3foo3Foo")
check('function', 'int foo(A a = x(a))', "foo__A", "3foo1A")
raises(DefinitionError, parse, 'function', 'int foo(B b=x(a)')
raises(DefinitionError, parse, 'function', 'int foo)C c=x(a))')
raises(DefinitionError, parse, 'function', 'int foo(D d=x(a')
with pytest.raises(DefinitionError):
parse('function', 'int foo(B b=x(a)')
with pytest.raises(DefinitionError):
parse('function', 'int foo)C c=x(a))')
with pytest.raises(DefinitionError):
parse('function', 'int foo(D d=x(a')
check('function', 'int foo(const A&... a)', "foo__ACRDp", "3fooDpRK1A")
check('function', 'virtual void f()', "f", "1fv")
# test for ::nestedName, from issue 1738
@ -382,8 +387,10 @@ def test_templates():
check('function', "template<> void A()", None, "IE1Av")
check('member', "template<> A a", None, "IE1a")
check('type', "template<> a = A", None, "IE1a")
raises(DefinitionError, parse, 'enum', "template<> A")
raises(DefinitionError, parse, 'enumerator', "template<> A")
with pytest.raises(DefinitionError):
parse('enum', "template<> A")
with pytest.raises(DefinitionError):
parse('enumerator', "template<> A")
# then all the real tests
check('class', "template<typename T1, typename T2> A", None, "I00E1A")
check('type', "template<> a", None, "IE1a")
@ -419,8 +426,10 @@ def test_templates():
"RK18c_string_view_baseIK4Char6TraitsE")
# template introductions
raises(DefinitionError, parse, 'enum', 'abc::ns::foo{id_0, id_1, id_2} A')
raises(DefinitionError, parse, 'enumerator', 'abc::ns::foo{id_0, id_1, id_2} A')
with pytest.raises(DefinitionError):
parse('enum', 'abc::ns::foo{id_0, id_1, id_2} A')
with pytest.raises(DefinitionError):
parse('enumerator', 'abc::ns::foo{id_0, id_1, id_2} A')
check('class', 'abc::ns::foo{id_0, id_1, id_2} xyz::bar',
None, 'I000EXN3abc2ns3fooEI4id_04id_14id_2EEN3xyz3barE')
check('class', 'abc::ns::foo{id_0, id_1, ...id_2} xyz::bar',
@ -469,12 +478,18 @@ def test_attributes():
check('member', 'paren_attr(a) int f', 'f__i', '1f')
check('member', 'paren_attr("") int f', 'f__i', '1f')
check('member', 'paren_attr(()[{}][]{}) int f', 'f__i', '1f')
raises(DefinitionError, parse, 'member', 'paren_attr(() int f')
raises(DefinitionError, parse, 'member', 'paren_attr([) int f')
raises(DefinitionError, parse, 'member', 'paren_attr({) int f')
raises(DefinitionError, parse, 'member', 'paren_attr([)]) int f')
raises(DefinitionError, parse, 'member', 'paren_attr((])) int f')
raises(DefinitionError, parse, 'member', 'paren_attr({]}) int f')
with pytest.raises(DefinitionError):
parse('member', 'paren_attr(() int f')
with pytest.raises(DefinitionError):
parse('member', 'paren_attr([) int f')
with pytest.raises(DefinitionError):
parse('member', 'paren_attr({) int f')
with pytest.raises(DefinitionError):
parse('member', 'paren_attr([)]) int f')
with pytest.raises(DefinitionError):
parse('member', 'paren_attr((])) int f')
with pytest.raises(DefinitionError):
parse('member', 'paren_attr({]}) int f')
# position: decl specs
check('function', 'static inline __attribute__(()) void f()',

View File

@ -11,7 +11,7 @@
import re
import sys
from util import with_app, rootdir, raises
from util import with_app, rootdir
from sphinx.ext.inheritance_diagram import InheritanceException, import_classes
import pytest
@ -53,8 +53,10 @@ def test_import_classes():
from example.sphinx import DummyClass
# got exception for unknown class or module
raises(InheritanceException, import_classes, 'unknown', None)
raises(InheritanceException, import_classes, 'unknown.Unknown', None)
with pytest.raises(InheritanceException):
import_classes('unknown', None)
with pytest.raises(InheritanceException):
import_classes('unknown.Unknown', None)
# a module having no classes
classes = import_classes('sphinx', None)
@ -80,7 +82,8 @@ def test_import_classes():
assert classes == [CatalogInfo]
# got exception for functions
raises(InheritanceException, import_classes, 'encode_uri', 'sphinx.util')
with pytest.raises(InheritanceException):
import_classes('encode_uri', 'sphinx.util')
# import submodule on current module (refs: #3164)
classes = import_classes('sphinx', 'example')

View File

@ -14,8 +14,9 @@ import time
from six import PY2, text_type, StringIO
from six.moves import input
import pytest
from util import raises, SkipTest
from util import SkipTest
from sphinx import application
from sphinx import quickstart as qs
@ -107,7 +108,8 @@ def test_do_prompt():
assert d['k4'] is True
qs.do_prompt(d, 'k5', 'Q5', validator=qs.boolean)
assert d['k5'] is False
raises(AssertionError, qs.do_prompt, d, 'k6', 'Q6', validator=qs.boolean)
with pytest.raises(AssertionError):
qs.do_prompt(d, 'k6', 'Q6', validator=qs.boolean)
def test_do_prompt_with_nonascii():

View File

@ -13,10 +13,11 @@ import os
import zipfile
import mock
import pytest
from sphinx.theming import Theme, ThemeError
from util import with_app, raises, path
from util import with_app, path
@with_app(confoverrides={'html_theme': 'ziptheme',
@ -46,10 +47,12 @@ def test_theme_api(app, status, warning):
assert theme.get_confstr('options', 'nosidebar') == 'false'
# nonexisting setting
assert theme.get_confstr('theme', 'foobar', 'def') == 'def'
raises(ThemeError, theme.get_confstr, 'theme', 'foobar')
with pytest.raises(ThemeError):
theme.get_confstr('theme', 'foobar')
# options API
raises(ThemeError, theme.get_options, {'nonexisting': 'foo'})
with pytest.raises(ThemeError):
theme.get_options({'nonexisting': 'foo'})
options = theme.get_options(cfg.html_theme_options)
assert options['testopt'] == 'foo'
assert options['nosidebar'] == 'false'

View File

@ -17,8 +17,9 @@ from os import path
from babel.messages.mofile import read_mo
from sphinx.util import i18n
from sphinx.errors import SphinxError
import pytest
from util import TestApp, raises
from util import TestApp
def test_catalog_info_for_file_and_path():
@ -261,4 +262,5 @@ def test_get_filename_for_language():
# invalid figure_language_filename
app.env.config.figure_language_filename = '{root}.{invalid}{ext}'
raises(SphinxError, i18n.get_image_filename_for_language, 'foo.png', app.env)
with pytest.raises(SphinxError):
i18n.get_image_filename_for_language('foo.png', app.env)

View File

@ -23,7 +23,7 @@ except ImportError:
sqlalchemy_missing = True
import pytest
from util import rootdir, tempdir, raises, skip_if
from util import rootdir, tempdir, skip_if
@pytest.fixture
@ -53,7 +53,8 @@ class NullStorage(StorageBackend):
@with_support(storage=NullStorage())
def test_no_srcdir(support):
# make sure the correct exception is raised if srcdir is not given.
raises(RuntimeError, support.build)
with pytest.raises(RuntimeError):
support.build()
@skip_if(sqlalchemy_missing, 'needs sqlalchemy')
@ -65,7 +66,8 @@ def test_build(support):
@skip_if(sqlalchemy_missing, 'needs sqlalchemy')
@with_support()
def test_get_document(support):
raises(DocumentNotFoundError, support.get_document, 'nonexisting')
with pytest.raises(DocumentNotFoundError):
support.get_document('nonexisting')
contents = support.get_document('contents')
assert contents['title'] and contents['body'] \
@ -90,8 +92,8 @@ def test_comments(support):
# Make sure that comments can't be added to a comment where
# displayed == False, since it could break the algorithm that
# converts a nodes comments to a tree.
raises(CommentNotAllowedError, support.add_comment, 'Not allowed',
parent_id=str(hidden_comment['id']))
with pytest.raises(CommentNotAllowedError):
support.add_comment('Not allowed', parent_id=str(hidden_comment['id']))
# Add a displayed and not displayed child to the displayed comment.
support.add_comment('Child test comment', parent_id=str(comment['id']),
username='user_one')
@ -133,8 +135,8 @@ def test_user_delete_comments(support):
comment = get_comment()
assert comment['username'] == 'user_one'
# Make sure other normal users can't delete someone elses comments.
raises(UserNotAuthorizedError, support.delete_comment,
comment['id'], username='user_two')
with pytest.raises(UserNotAuthorizedError):
support.delete_comment(comment['id'], username='user_two')
# Now delete the comment using the correct username.
support.delete_comment(comment['id'], username='user_one')
comment = get_comment()
@ -164,8 +166,10 @@ def test_moderation(support):
# Make sure the moderation_callback is called.
assert called
# Make sure the user must be a moderator.
raises(UserNotAuthorizedError, support.accept_comment, accepted['id'])
raises(UserNotAuthorizedError, support.delete_comment, deleted['id'])
with pytest.raises(UserNotAuthorizedError):
support.accept_comment(accepted['id'])
with pytest.raises(UserNotAuthorizedError):
support.delete_comment(deleted['id'])
support.accept_comment(accepted['id'], moderator=True)
support.delete_comment(deleted['id'], moderator=True)
comments = support.get_data(node.id)['comments']
@ -186,7 +190,8 @@ def test_moderator_delete_comments(support):
comment = get_comment()
support.delete_comment(comment['id'], username='user_two',
moderator=True)
raises(IndexError, get_comment)
with pytest.raises(IndexError):
get_comment()
@skip_if(sqlalchemy_missing, 'needs sqlalchemy')
@ -248,8 +253,10 @@ def test_voting(support):
check_rating(2)
# Make sure a vote with value > 1 or < -1 can't be cast.
raises(ValueError, support.process_vote, comment['id'], 'user_one', '2')
raises(ValueError, support.process_vote, comment['id'], 'user_one', '-2')
with pytest.raises(ValueError):
support.process_vote(comment['id'], 'user_one', '2')
with pytest.raises(ValueError):
support.process_vote(comment['id'], 'user_one', '-2')
# Make sure past voting data is associated with comments when they are
# fetched.

View File

@ -11,7 +11,7 @@
from __future__ import print_function
from sphinx.writers.latex import rstdim_to_latexdim
from util import raises
import pytest
def test_rstdim_to_latexdim():
@ -32,5 +32,6 @@ def test_rstdim_to_latexdim():
assert rstdim_to_latexdim('.5em') == '.5em'
# unknown values (it might be generated by 3rd party extension)
raises(ValueError, rstdim_to_latexdim, 'unknown')
with pytest.raises(ValueError):
rstdim_to_latexdim('unknown')
assert rstdim_to_latexdim('160.0unknown') == '160.0unknown'