Reputation: 73
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)
Upvotes: 2
Views: 154
Reputation: 77942
The reason you have this issue is that, in Py3k, looking up a function on a class now yields the original function instead of an unboundmethod. In Py2:
class Foo(object):
def bar(self):
pass
type(Foo.bar)
<type 'instancemethod'>
In Python3
class Foo:
def bar(self):
pass
type(Foo.bar)
<class 'function'>
So the solution is dead simple: you just don't need the .im_func
part. Also, function.func_code
is now named function.__code__
. So you want something like (caveat: untested code):
ln = lambda f: getattr(Login, f).__code__.co_firstlineno
FWIW, you could have debugged this by yourself just the way I did: inspecting things in your Python shell (took me about 2 minutes to find this out xD).
Upvotes: 2