Dynamically adding methods to a class in Python
Date : March 29 2020, 07:55 AM
|
unittesting(Python), test if function returns an array
Date : March 29 2020, 07:55 AM
Any of those help I have a python function somefunc(), that must return an array like this [1, 2, 3]. Now I'm writing a unittest to check what this functions return: , You can write: from mymodule import somefunc
class TestingFunctionsTest(unittest.TestCase):
def test_somefunc(self):
self.assertEqual(somefunc(), [1, 2, 3])
|
Adding test methods dynamically using decorator
Date : March 29 2020, 07:55 AM
hop of those help? The best solution is to use the sub tests feature of unittest in python 3.4. Documentation found here and used like: class NumbersTest(unittest.TestCase):
def test_even(self):
"""
Test that numbers between 0 and 5 are all even.
"""
for i in range(0, 6):
with self.subTest(i=i):
self.assertEqual(i % 2, 0)
class sub_test_data(object):
def __init__(self, *test_data):
self.test_data = test_data
def __call__(self, func):
func.sub_test_data = self.test_data
func.has_sub_tests = True
return func
def create_test_driver(func, *args):
def test_driver(self):
try:
func(self, *args)
except AssertionError as e:
e.args += ({"test_args": args},)
raise
return test_driver
def create_sub_tests(cls):
for attr_name, func in list(vars(cls).items()):
if getattr(func, "has_sub_tests", False):
for i, value in enumerate(func.sub_test_data):
test_name = 'test_{}_subtest{}'.format(attr_name, i)
setattr(cls, test_name, create_test_driver(func, value))
return cls
@create_sub_tests
class NumbersTest(unittest.TestCase):
tickets = [0, 1, 2, 3, 4, 5]
@sub_test_data(*tickets)
def even(self, t):
self.assertEqual(t % 2, 0)
|
Python unittesting: Test whether two angles are almost equal
Date : March 29 2020, 07:55 AM
Does that help You can use the squared Euclidian distance between two points on the unit circle and the law of cosines to get the absolute difference between two angles: from math import sin, cos, acos
from unittest import assertAlmostEqual
def assertAlmostEqualAngles(x, y, **kwargs):
c2 = (sin(x)-sin(y))**2 + (cos(x)-cos(y))**2
angle_diff = acos((2.0 - c2)/2.0) # a = b = 1
assertAlmostEqual(angle_diff, 0.0, **kwargs)
from math import sin, cos, acos, radians, degrees
from unittest import assertAlmostEqual
def assertAlmostEqualAngles(x, y, **kwargs):
x,y = radians(x),radians(y)
c2 = (sin(x)-sin(y))**2 + (cos(x)-cos(y))**2
angle_diff = degrees(acos((2.0 - c2)/2.0))
assertAlmostEqual(angle_diff, 0.0, **kwargs)
|
How to set test group to @Test methods dynamically using TestNG?
Tag : java , By : user119605
Date : March 29 2020, 07:55 AM
like below fixes the issue You should perhaps start exploring the beanshell way of method selection that TestNG provides to you for this purpose. Sometime back I wrote up a blog post which talks about how to work with Beanshell expressions in TestNG. You can read more about it here and refer to the official TestNG documentation here. <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="1265_Suite" parallel="false" verbose="2">
<test name="92" parallel="false" preserve-order="true">
<method-selectors>
<method-selector>
<script language="beanshell">
<![CDATA[whatGroup = System.getProperty("groupToRun");
(groups.containsKey(whatGroup) || testngMethod.getGroups().length ==0);
]]>
</script>
</method-selector>
</method-selectors>
<classes>
<class name="com.rationaleemotions.stackoverflow.MyTest1"/>
<class name="com.rationaleemotions.stackoverflow.MyTest2"/>
</classes>
</test>
</suite>
mvn clean test -DsuiteXmlFile=dynamic_groups.xml -DgroupToRun=group2
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running TestSuite
...
... TestNG 6.11 by Cédric Beust (cedric@beust.com)
...
test1 called
test3 called
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 15.377 sec - in TestSuite
Results :
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
|