Skip to content

Commit

Permalink
[qacode] fix for issue #279, for master branch (#311)
Browse files Browse the repository at this point in the history
* [qacode] fix for issue #279, for master branch

* [qacode] fixes for flake
  • Loading branch information
netzulo authored Apr 6, 2020
1 parent a1ff568 commit 52a1213
Show file tree
Hide file tree
Showing 20 changed files with 681 additions and 441 deletions.
55 changes: 31 additions & 24 deletions USAGE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -259,26 +259,22 @@ TestInfoBase
+ method **sleep** : Just call to native python time.sleep method
- Methods for **Asserts**

+ method **assert_equals** : Allow to compare 2 values and check if 1st it's equals to 2nd value
+ method **assert_not_equals** : Allow to compare 2 value to check if 1st isn't equals to 2nd value
+ method **assert_equals_url** : Allow to compare 2 urls and check if 1st it's equals to 2nd url
+ method **assert_not_equals_url** : Allow to compare 2 urls to check if 1st isn't equals to 2nd url
+ method **assert_contains_url** : Allow to compare 2 urls and check if 1st contains 2nd url
+ method **assert_not_contains_url** : Allow to compare 2 urls and check if 1st not contains 2nd url
+ method **assert_is_instance** : Allow to encapsulate method assertIsInstance(obj, cls, msg='')
+ method **assert_greater** : Allow to encapsulate method assertGreater(a, b, msg=msg)
+ method **assert_lower** : Allow to encapsulate method assertLower(a, b, msg=msg)
+ method **assert_in** : Allow to compare if value it's in to 2nd list of values
+ method **assert_not_in** : Allow to compare if value it's not in to 2nd list of values
+ method **assert_regex** : Allow to compare if value match pattern
+ method **assert_not_regex** : Allow to compare if value not match pattern
+ method **assert_regex_url** : Allow to compare if value match url pattern, can use custom pattern
+ method **assert_path_exist** : Allow to check if path exist, can check if is_dir also
+ method **assert_path_not_exist** : Allow to check if path not exist, can check if is_dir also
+ method **assert_true** : Allow to compare and check if value it's equals to 'True'
+ method **assert_false** : Allow to compare and check if value it's equals to 'False'
+ method **assert_none** : Allow to compare and check if value it's equals to 'None'
+ method **assert_not_none** : Allow to compare and check if value it's not equals to 'None'
+ method **equals** : Allow to compare 2 values and check if 1st it's equals to 2nd value
+ method **not_equals** : Allow to compare 2 value to check if 1st isn't equals to 2nd value
+ method **is_instance** : Allow to encapsulate method assertIsInstance(obj, cls, msg='')
+ method **greater** : Allow to encapsulate method assertGreater(a, b, msg=msg)
+ method **lower** : Allow to encapsulate method assertLower(a, b, msg=msg)
+ method **in_list** : Allow to compare if value it's in to 2nd list of values
+ method **not_in_list** : Allow to compare if value it's not in to 2nd list of values
+ method **regex** : Allow to compare if value match pattern
+ method **not_regex** : Allow to compare if value not match pattern
+ method **regex_url** : Allow to compare if value match url pattern, can use custom pattern
+ method **path_exist** : Allow to check if path exist, can check if is_dir also
+ method **path_not_exist** : Allow to check if path not exist, can check if is_dir also
+ method **true** : Allow to compare and check if value it's equals to 'True'
+ method **false** : Allow to compare and check if value it's equals to 'False'
+ method **none** : Allow to compare and check if value it's equals to 'None'
+ method **not_none** : Allow to compare and check if value it's not equals to 'None'


Example : inherit from TestInfoBase class
Expand All @@ -289,17 +285,21 @@ Example : inherit from TestInfoBase class
from qacode.utils import settings
from qacode.core.bots import BotBase
from qacode.core.testing.asserts import Assert
from qacode.core.testing.test_info import TestInfoBase
ASSERT = Assert()
class TestAwesome(TestInfoBase):
def test_some_method(self):
try:
_settings = settings('settings.json')
bot = self.bot_open(**_settings)
self.log.info("Bot opened for new test method down new test suite")
self.assert_is_instance(bot, BotBase)
ASSERT.is_instance(bot, BotBase)
except AssertionError as err:
self.log.error("Bot Fails at assert %s", err.message)
Expand All @@ -319,14 +319,18 @@ Example : inherit from TestInfoBot class
.. code:: python
from qacode.core.testing.asserts import Assert
from qacode.core.testing.test_info import TestInfoBot
ASSERT = Assert()
class TestAwesome(TestInfoBot):
def test_some_method(self):
try:
self.assert_is_instance(self.bot, BotBase)
ASSERT.is_instance(self.bot, BotBase)
except AssertionError as err:
self.log.error("Bot Fails at assert %s", err.message)
Expand All @@ -349,20 +353,23 @@ Example : inherit from TestInfoBotUnique class
.. code:: python
from qacode.core.testing.asserts import Assert
from qacode.core.testing.test_info import TestInfoBotUnique
ASSERT = Assert()
class TestAwesomeUnique(TestInfoBotUnique):
def test_some_method(self):
try:
self.assert_is_instance(self.bot, BotBase)
ASSERT.is_instance(self.bot, BotBase)
except AssertionError as err:
self.log.error("Bot Fails at assert %s", err.message)
def test_some_another_method(self):
try:
# Same bot that was used for 'test_some_method' test
self.assert_is_instance(self.bot, BotBase)
ASSERT.is_instance(self.bot, BotBase)
except AssertionError as err:
self.log.error("Bot Fails at assert %s", err.message)
2 changes: 1 addition & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
addopts = --verbose
testpaths = tests
console_output_style = progress
python_files = suite_*_*.py
python_files = suite_*.py
python_classes = Test*
python_functions = test_*_*
filterwarnings =
Expand Down
3 changes: 2 additions & 1 deletion qacode/core/testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
"""package qacode.core.testing"""


from qacode.core.testing import asserts
from qacode.core.testing import test_info
from qacode.core.testing import testlink


__all__ = ['test_info', 'testlink']
__all__ = ['asserts', 'test_info', 'testlink']
160 changes: 160 additions & 0 deletions qacode/core/testing/asserts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
# -*- coding: utf-8 -*-
"""Base module for asserts on Test Suites"""


import os
import re


ASSERT_MSG_DEFAULT = "Fails at '{}': actual={}, expected={}"
ASSERT_REGEX_URL = r"http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+" # noqa: E501


class Assert(object):
"""Base class for inherit new Test classes"""

@classmethod
def message(cls, assert_name, actual, expected, msg=None):
"""Generate assert message for method that calls for it
Arguments:
assert_name {str} -- Assert method name that call
actual {any} -- Actual value to compare
expected {any} -- Expected value to compare
Keyword Arguments:
msg {[type]} -- [description] (default: {None})
Returns:
str -- Message to be use on Assert method
"""
if msg is not None:
return msg
return ASSERT_MSG_DEFAULT.format(
assert_name,
actual,
expected)

def equals(self, actual, expected, msg=None):
"""Allow to compare 2 values and check if 1st it's equals to
2nd value
"""
_msg = self.message("assert_equals", actual, expected, msg=msg)
if actual != expected:
raise AssertionError(actual, expected, _msg)
return True

def not_equals(self, actual, expected, msg=None):
"""Allow to compare 2 value to check if 1st isn't equals to
2nd value
"""
_msg = self.message("assert_not_equals", actual, expected, msg=msg)
if actual == expected:
raise AssertionError(actual, expected, _msg)
return True

def is_instance(self, instance, class_type, msg=None):
"""Allow to encapsulate method assertIsInstance(obj, cls, msg='')"""
_msg = self.message(
"assert_is_instance", instance, class_type, msg=msg)
if not isinstance(class_type, type):
class_type = type(class_type)
if not isinstance(instance, class_type):
raise AssertionError(instance, class_type, _msg)
return True

def greater(self, actual, greater, msg=None):
"""Allow to encapsulate method assertGreater(a, b, msg=msg)"""
_msg = self.message("assert_greater", actual, greater, msg=msg)
if actual < greater:
raise AssertionError(actual, greater, _msg)
return True

def lower(self, actual, lower, msg=None):
"""Allow to encapsulate method assertLower(a, b, msg=msg)"""
_msg = self.message("assert_lower", actual, lower, msg=msg)
if actual > lower:
raise AssertionError(actual, lower, _msg)
return True

def greater_or_equals(self, actual, greater, msg=None):
"""Allow to compare if A it's greater or equals than B"""
_msg = self.message(
"assert_greater_or_equals", actual, greater, msg=msg)
if actual <= greater:
raise AssertionError(actual, greater, _msg)
return True

def lower_or_equals(self, actual, lower, msg=None):
"""Allow to compare if A it's lower or equals than B"""
_msg = self.message("assert_lower_or_equals", actual, lower, msg=msg)
if actual >= lower:
raise AssertionError(actual, lower, _msg)
return True

def in_list(self, actual, valid_values, msg=None):
"""Allow to compare if value it's in to 2nd list of values"""
_msg = self.message("assert_in_list", actual, valid_values, msg=msg)
if actual not in valid_values:
raise AssertionError(actual, valid_values, _msg)
return True

def not_in_list(self, actual, invalid_values, msg=None):
"""Allow to compare if value it's not in to 2nd list of values"""
_msg = self.message(
"assert_not_in_list", actual, invalid_values, msg=msg)
if actual in invalid_values:
raise AssertionError(actual, invalid_values, _msg)
return True

def regex(self, actual, pattern, msg=None):
"""Allow to compare if value match pattern"""
_msg = self.message("assert_regex", actual, pattern, msg=msg)
is_match = re.match(pattern, actual)
if not is_match:
raise AssertionError(actual, pattern, _msg)
return True

def not_regex(self, actual, pattern, msg=None):
"""Allow to compare if value not match pattern"""
_msg = self.message("assert_not_regex", actual, pattern, msg=msg)
is_match = re.match(pattern, actual)
if is_match:
raise AssertionError(actual, pattern, _msg)
return True

def regex_url(self, actual, msg=None):
"""Allow to compare if value match url pattern, can use
custom pattern
"""
return self.regex(actual, ASSERT_REGEX_URL, msg=msg)

def path_exist(self, actual, msg=None):
"""Allow to check if path exist, can check if is_dir also"""
_msg = self.message("assert_path_exist", actual, "", msg=msg)
if not os.path.exists(actual):
raise AssertionError(actual, "PATH_NOT_EXIST", _msg)
_is_dir = os.path.isdir(actual)
if not _is_dir:
raise AssertionError(actual, "PATH_NOT_DIR", _msg)
return True

def path_not_exist(self, actual, msg=None):
"""Allow to check if path not exist, can check if is_dir also"""
_msg = self.message("assert_path_not_exist", actual, "", msg=msg)
if os.path.exists(actual):
raise AssertionError(actual, "PATH_EXIST_AND_MUST_NOT", _msg)
return True

def true(self, actual, msg=None):
"""Allow to compare and check if value it's equals to 'True'"""
return self.equals(actual, True, msg=msg)

def false(self, actual, msg=None):
"""Allow to compare and check if value it's equals to 'False'"""
return self.equals(actual, False, msg=msg)

def none(self, actual, msg=None):
"""Allow to compare and check if value it's equals to 'None'"""
return self.equals(actual, None, msg=msg)

def not_none(self, actual, msg=None):
"""Allow to compare and check if value it's not equals to 'None'"""
return self.not_equals(actual, None, msg=msg)
Loading

0 comments on commit 52a1213

Please sign in to comment.