2

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)
andrew_k
  • 73
  • 5
  • 1
    Just remove the requirement that the tests should run in a particular order. – quamrana Oct 10 '19 at 13:10
  • @quamrana what do you mean remove the requirement that the tests should run in a particular order? can you tell me please which line I need to fix? – andrew_k Oct 10 '19 at 13:13
  • Can you provide the full traceback for the error? – Tom Dalton Oct 10 '19 at 13:39
  • @TomDalton `ln = lambda f: getattr(Login, f).im_func.func_code.co_firstlineno AttributeError: 'function' object has no attribute 'im_func'` – andrew_k Oct 10 '19 at 13:40
  • 1
    @andrew_k your tests should NOT depend on a particular order - they all should be totally independant and give the exact same results whatever the execution order. – bruno desthuilliers Oct 10 '19 at 13:58
  • @brunodesthuilliers I agree with you, but all my tests is for appium. In the appium is test steps for example `test_press_on_button` then `test_load_pic` etc and then I can see which test was fail. – andrew_k Oct 10 '19 at 14:05
  • 1
    @andrew_k then you definitly want to edit your question to make this clear. And possibly either search for a more appropriate testing tool, or rewrite your tests so _each_ test method performs a full sequence of steps (if you have common steps, factor them out to helper methods that you call from the `test_XXX` ones). – bruno desthuilliers Oct 10 '19 at 14:08
  • @brunodesthuilliers but I found the solution for my problem but it's works on the python 2 so only that I need some help to solve this problem for python 3 – andrew_k Oct 10 '19 at 14:10

1 Answers1

2

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).

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118