Skip to content

Commit 52a1213

Browse files
authored
[qacode] fix for issue #279, for master branch (#311)
* [qacode] fix for issue #279, for master branch * [qacode] fixes for flake
1 parent a1ff568 commit 52a1213

20 files changed

+681
-441
lines changed

USAGE.rst

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -259,26 +259,22 @@ TestInfoBase
259259
+ method **sleep** : Just call to native python time.sleep method
260260
- Methods for **Asserts**
261261

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

283279

284280
Example : inherit from TestInfoBase class
@@ -289,17 +285,21 @@ Example : inherit from TestInfoBase class
289285
290286
from qacode.utils import settings
291287
from qacode.core.bots import BotBase
288+
from qacode.core.testing.asserts import Assert
292289
from qacode.core.testing.test_info import TestInfoBase
293290
294291
292+
ASSERT = Assert()
293+
294+
295295
class TestAwesome(TestInfoBase):
296296
297297
def test_some_method(self):
298298
try:
299299
_settings = settings('settings.json')
300300
bot = self.bot_open(**_settings)
301301
self.log.info("Bot opened for new test method down new test suite")
302-
self.assert_is_instance(bot, BotBase)
302+
ASSERT.is_instance(bot, BotBase)
303303
except AssertionError as err:
304304
self.log.error("Bot Fails at assert %s", err.message)
305305
@@ -319,14 +319,18 @@ Example : inherit from TestInfoBot class
319319
.. code:: python
320320
321321
322+
from qacode.core.testing.asserts import Assert
322323
from qacode.core.testing.test_info import TestInfoBot
323324
324325
326+
ASSERT = Assert()
327+
328+
325329
class TestAwesome(TestInfoBot):
326330
327331
def test_some_method(self):
328332
try:
329-
self.assert_is_instance(self.bot, BotBase)
333+
ASSERT.is_instance(self.bot, BotBase)
330334
except AssertionError as err:
331335
self.log.error("Bot Fails at assert %s", err.message)
332336
@@ -349,20 +353,23 @@ Example : inherit from TestInfoBotUnique class
349353
.. code:: python
350354
351355
356+
from qacode.core.testing.asserts import Assert
352357
from qacode.core.testing.test_info import TestInfoBotUnique
353358
354359
360+
ASSERT = Assert()
361+
355362
class TestAwesomeUnique(TestInfoBotUnique):
356363
357364
def test_some_method(self):
358365
try:
359-
self.assert_is_instance(self.bot, BotBase)
366+
ASSERT.is_instance(self.bot, BotBase)
360367
except AssertionError as err:
361368
self.log.error("Bot Fails at assert %s", err.message)
362369
363370
def test_some_another_method(self):
364371
try:
365372
# Same bot that was used for 'test_some_method' test
366-
self.assert_is_instance(self.bot, BotBase)
373+
ASSERT.is_instance(self.bot, BotBase)
367374
except AssertionError as err:
368375
self.log.error("Bot Fails at assert %s", err.message)

pytest.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
addopts = --verbose
33
testpaths = tests
44
console_output_style = progress
5-
python_files = suite_*_*.py
5+
python_files = suite_*.py
66
python_classes = Test*
77
python_functions = test_*_*
88
filterwarnings =

qacode/core/testing/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
"""package qacode.core.testing"""
33

44

5+
from qacode.core.testing import asserts
56
from qacode.core.testing import test_info
67
from qacode.core.testing import testlink
78

89

9-
__all__ = ['test_info', 'testlink']
10+
__all__ = ['asserts', 'test_info', 'testlink']

qacode/core/testing/asserts.py

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# -*- coding: utf-8 -*-
2+
"""Base module for asserts on Test Suites"""
3+
4+
5+
import os
6+
import re
7+
8+
9+
ASSERT_MSG_DEFAULT = "Fails at '{}': actual={}, expected={}"
10+
ASSERT_REGEX_URL = r"http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+" # noqa: E501
11+
12+
13+
class Assert(object):
14+
"""Base class for inherit new Test classes"""
15+
16+
@classmethod
17+
def message(cls, assert_name, actual, expected, msg=None):
18+
"""Generate assert message for method that calls for it
19+
Arguments:
20+
assert_name {str} -- Assert method name that call
21+
actual {any} -- Actual value to compare
22+
expected {any} -- Expected value to compare
23+
Keyword Arguments:
24+
msg {[type]} -- [description] (default: {None})
25+
Returns:
26+
str -- Message to be use on Assert method
27+
"""
28+
if msg is not None:
29+
return msg
30+
return ASSERT_MSG_DEFAULT.format(
31+
assert_name,
32+
actual,
33+
expected)
34+
35+
def equals(self, actual, expected, msg=None):
36+
"""Allow to compare 2 values and check if 1st it's equals to
37+
2nd value
38+
"""
39+
_msg = self.message("assert_equals", actual, expected, msg=msg)
40+
if actual != expected:
41+
raise AssertionError(actual, expected, _msg)
42+
return True
43+
44+
def not_equals(self, actual, expected, msg=None):
45+
"""Allow to compare 2 value to check if 1st isn't equals to
46+
2nd value
47+
"""
48+
_msg = self.message("assert_not_equals", actual, expected, msg=msg)
49+
if actual == expected:
50+
raise AssertionError(actual, expected, _msg)
51+
return True
52+
53+
def is_instance(self, instance, class_type, msg=None):
54+
"""Allow to encapsulate method assertIsInstance(obj, cls, msg='')"""
55+
_msg = self.message(
56+
"assert_is_instance", instance, class_type, msg=msg)
57+
if not isinstance(class_type, type):
58+
class_type = type(class_type)
59+
if not isinstance(instance, class_type):
60+
raise AssertionError(instance, class_type, _msg)
61+
return True
62+
63+
def greater(self, actual, greater, msg=None):
64+
"""Allow to encapsulate method assertGreater(a, b, msg=msg)"""
65+
_msg = self.message("assert_greater", actual, greater, msg=msg)
66+
if actual < greater:
67+
raise AssertionError(actual, greater, _msg)
68+
return True
69+
70+
def lower(self, actual, lower, msg=None):
71+
"""Allow to encapsulate method assertLower(a, b, msg=msg)"""
72+
_msg = self.message("assert_lower", actual, lower, msg=msg)
73+
if actual > lower:
74+
raise AssertionError(actual, lower, _msg)
75+
return True
76+
77+
def greater_or_equals(self, actual, greater, msg=None):
78+
"""Allow to compare if A it's greater or equals than B"""
79+
_msg = self.message(
80+
"assert_greater_or_equals", actual, greater, msg=msg)
81+
if actual <= greater:
82+
raise AssertionError(actual, greater, _msg)
83+
return True
84+
85+
def lower_or_equals(self, actual, lower, msg=None):
86+
"""Allow to compare if A it's lower or equals than B"""
87+
_msg = self.message("assert_lower_or_equals", actual, lower, msg=msg)
88+
if actual >= lower:
89+
raise AssertionError(actual, lower, _msg)
90+
return True
91+
92+
def in_list(self, actual, valid_values, msg=None):
93+
"""Allow to compare if value it's in to 2nd list of values"""
94+
_msg = self.message("assert_in_list", actual, valid_values, msg=msg)
95+
if actual not in valid_values:
96+
raise AssertionError(actual, valid_values, _msg)
97+
return True
98+
99+
def not_in_list(self, actual, invalid_values, msg=None):
100+
"""Allow to compare if value it's not in to 2nd list of values"""
101+
_msg = self.message(
102+
"assert_not_in_list", actual, invalid_values, msg=msg)
103+
if actual in invalid_values:
104+
raise AssertionError(actual, invalid_values, _msg)
105+
return True
106+
107+
def regex(self, actual, pattern, msg=None):
108+
"""Allow to compare if value match pattern"""
109+
_msg = self.message("assert_regex", actual, pattern, msg=msg)
110+
is_match = re.match(pattern, actual)
111+
if not is_match:
112+
raise AssertionError(actual, pattern, _msg)
113+
return True
114+
115+
def not_regex(self, actual, pattern, msg=None):
116+
"""Allow to compare if value not match pattern"""
117+
_msg = self.message("assert_not_regex", actual, pattern, msg=msg)
118+
is_match = re.match(pattern, actual)
119+
if is_match:
120+
raise AssertionError(actual, pattern, _msg)
121+
return True
122+
123+
def regex_url(self, actual, msg=None):
124+
"""Allow to compare if value match url pattern, can use
125+
custom pattern
126+
"""
127+
return self.regex(actual, ASSERT_REGEX_URL, msg=msg)
128+
129+
def path_exist(self, actual, msg=None):
130+
"""Allow to check if path exist, can check if is_dir also"""
131+
_msg = self.message("assert_path_exist", actual, "", msg=msg)
132+
if not os.path.exists(actual):
133+
raise AssertionError(actual, "PATH_NOT_EXIST", _msg)
134+
_is_dir = os.path.isdir(actual)
135+
if not _is_dir:
136+
raise AssertionError(actual, "PATH_NOT_DIR", _msg)
137+
return True
138+
139+
def path_not_exist(self, actual, msg=None):
140+
"""Allow to check if path not exist, can check if is_dir also"""
141+
_msg = self.message("assert_path_not_exist", actual, "", msg=msg)
142+
if os.path.exists(actual):
143+
raise AssertionError(actual, "PATH_EXIST_AND_MUST_NOT", _msg)
144+
return True
145+
146+
def true(self, actual, msg=None):
147+
"""Allow to compare and check if value it's equals to 'True'"""
148+
return self.equals(actual, True, msg=msg)
149+
150+
def false(self, actual, msg=None):
151+
"""Allow to compare and check if value it's equals to 'False'"""
152+
return self.equals(actual, False, msg=msg)
153+
154+
def none(self, actual, msg=None):
155+
"""Allow to compare and check if value it's equals to 'None'"""
156+
return self.equals(actual, None, msg=msg)
157+
158+
def not_none(self, actual, msg=None):
159+
"""Allow to compare and check if value it's not equals to 'None'"""
160+
return self.not_equals(actual, None, msg=msg)

0 commit comments

Comments
 (0)