I'm using unittes to write my test cases. As I understand that the unittest handles the test classes and test methods inside the test classes in alphabetical order by default (even when loader.sortTestMethodsUsing is None). So I find some solution here and it's really works fine on python 2. When I try to run this solution on python 3 I get the error NameError: name 'cmp' is not defined
So I find the answer about how I can solve this problem here. I create function in another file and then I imported the cmp()
. But I still have problem to order my tests and I don't know why.
cmp_f.py
def cmp(a, b):
return (a > b) - (a < b)
test_f.py
import unittest
from cmp_f import cmp
class Login(unittest.TestCase):
def test_remove_notes_and_reports(self):
print("1")
def test_login_app(self):
print("2")
def test_report_summary_after_edit(self):
print("3")
def test_report_summary(self):
print("4")
if __name__ == "__main__":
loader = unittest.TestLoader()
ln = lambda f: getattr(Login, f).im_func.func_code.co_firstlineno
lncmp = lambda a, b: cmp(ln(a), ln(b))
loader.sortTestMethodsUsing = lncmp
unittest.main(testLoader=loader, verbosity=2)