From: Emanuele Rocca Date: Fri, 14 Oct 2022 21:22:50 +0200 Subject: replace nose assert_ with regular assertions or pytest equivalents --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,17 +1,11 @@ -#!/usr/bin/env python +def assert_equals(a, b): + assert a == b -import functools +def assert_equal(a, b): + assert a == b -from nose import SkipTest +def assert_true(a): + assert a is True - -def expected_failure(test): - @functools.wraps(test) - def inner(*args, **kwargs): - try: - test(*args, **kwargs) - except Exception: - raise SkipTest - else: - raise AssertionError('Failure expected') - return inner +def assert_false(a): + assert a is False --- a/tests/test_inputs.py +++ b/tests/test_inputs.py @@ -4,10 +4,11 @@ import pytz import re #noinspection PyUnresolvedReferences -from nose.tools import assert_equal, assert_raises # you need it for tests in form of continuations +from pytest import raises import six from flask_restful import inputs +from tests import assert_equal def test_reverse_rfc822_datetime(): @@ -119,7 +120,7 @@ def test_regex_bad_input(): num_only = inputs.regex(r'^[0-9]+$') for value in cases: - yield assert_raises, ValueError, lambda: num_only(value) + yield raises, ValueError, lambda: num_only(value) def test_regex_good_input(): @@ -137,7 +138,7 @@ def test_regex_good_input(): def test_regex_bad_pattern(): """Regex error raised immediately when regex input parser is created.""" - assert_raises(re.error, inputs.regex, '[') + raises(re.error, inputs.regex, '[') def test_regex_flags_good_input(): @@ -162,7 +163,7 @@ def test_regex_flags_bad_input(): case_sensitive = inputs.regex(r'^[A-Z]+$') for value in cases: - yield assert_raises, ValueError, lambda: case_sensitive(value) + yield raises, ValueError, lambda: case_sensitive(value) class TypesTestCase(unittest.TestCase): @@ -192,35 +193,35 @@ class TypesTestCase(unittest.TestCase): assert_equal(inputs.boolean(False), False) def test_bad_boolean(self): - assert_raises(ValueError, lambda: inputs.boolean("blah")) + raises(ValueError, lambda: inputs.boolean("blah")) def test_date_later_than_1900(self): assert_equal(inputs.date("1900-01-01"), datetime(1900, 1, 1)) def test_date_input_error(self): - assert_raises(ValueError, lambda: inputs.date("2008-13-13")) + raises(ValueError, lambda: inputs.date("2008-13-13")) def test_date_input(self): assert_equal(inputs.date("2008-08-01"), datetime(2008, 8, 1)) def test_natual_negative(self): - assert_raises(ValueError, lambda: inputs.natural(-1)) + raises(ValueError, lambda: inputs.natural(-1)) def test_natural(self): assert_equal(3, inputs.natural(3)) def test_natual_string(self): - assert_raises(ValueError, lambda: inputs.natural('foo')) + raises(ValueError, lambda: inputs.natural('foo')) def test_positive(self): assert_equal(1, inputs.positive(1)) assert_equal(10000, inputs.positive(10000)) def test_positive_zero(self): - assert_raises(ValueError, lambda: inputs.positive(0)) + raises(ValueError, lambda: inputs.positive(0)) def test_positive_negative_input(self): - assert_raises(ValueError, lambda: inputs.positive(-1)) + raises(ValueError, lambda: inputs.positive(-1)) def test_int_range_good(self): int_range = inputs.int_range(1, 5) @@ -232,11 +233,11 @@ class TypesTestCase(unittest.TestCase): def test_int_range_low(self): int_range = inputs.int_range(0, 5) - assert_raises(ValueError, lambda: int_range(-1)) + raises(ValueError, lambda: int_range(-1)) def test_int_range_high(self): int_range = inputs.int_range(0, 5) - assert_raises(ValueError, lambda: int_range(6)) + raises(ValueError, lambda: int_range(6)) def test_isointerval(): @@ -416,7 +417,7 @@ def test_bad_isointervals(): for bad_interval in bad_intervals: yield ( - assert_raises, + raises, Exception, inputs.iso8601interval, bad_interval, --- a/tests/test_api_with_blueprint.py +++ b/tests/test_api_with_blueprint.py @@ -9,8 +9,7 @@ import flask import flask_restful import flask_restful.fields #noinspection PyUnresolvedReferences -from nose.tools import assert_true, assert_false # you need it for tests in form of continuations - +from tests import assert_true, assert_false # Add a dummy Resource to verify that the app is properly set. class HelloWorld(flask_restful.Resource): --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -9,8 +9,7 @@ from flask_restful import fields from datetime import datetime, timedelta, tzinfo from flask import Flask, Blueprint #noinspection PyUnresolvedReferences -from nose.tools import assert_equals # you need it for tests in form of continuations - +from tests import assert_equals class Foo(object): def __init__(self): --- a/tests/test_api.py +++ b/tests/test_api.py @@ -16,7 +16,7 @@ import flask_restful import flask_restful.fields from flask_restful import OrderedDict from json import dumps, loads, JSONEncoder -from nose.tools import assert_equal # you need it for tests in form of continuations +from tests import assert_equal import six from types import SimpleNamespace from unittest.mock import patch