This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: can't run single lamba funcs as unittest
Type: enhancement Stage: test needed
Components: Library (Lib) Versions: Python 3.1, Python 2.7
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: georg.brandl, tctimmeh
Priority: normal Keywords:

Created on 2007-06-25 16:28 by tctimmeh, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (3)
msg32397 - (view) Author: timmeh (tctimmeh) Date: 2007-06-25 16:28
I have some code that adds new test funtions to a TestCase class as lambda functions:

ExmplTests.myTest = lambda: 1+2

When I run the app and specify my lamba test to run, as in:

myTestProg ExmplTests.mytest

I get:

Traceback (most recent call last):
  File "C:\work\CTTS\o-ticket\scOmniCTTS\test\otfbatch_test.py", line 188, in ?
    unittest.main(defaultTest = 'suite')
  File "C:\Python24\lib\unittest.py", line 761, in __init__
    self.parseArgs(argv)
  File "C:\Python24\lib\unittest.py", line 788, in parseArgs
    self.createTests()
  File "C:\Python24\lib\unittest.py", line 794, in createTests
    self.module)
  File "C:\Python24\lib\unittest.py", line 559, in loadTestsFromNames
    suites = [self.loadTestsFromName(name, module) for name in names]
  File "C:\Python24\lib\unittest.py", line 543, in loadTestsFromName
    return parent(obj.__name__)
  File "C:\Python24\lib\unittest.py", line 211, in __init__
    raise ValueError, "no such test method in %s: %s" % \
ValueError: no such test method in <class '__main__.ExmplTests'>: <lambda>

It seems this is the case because lamba functions are always named '<lamba>'.  If I change unittest.py ln543 from:
  return parent(obj.__name__)
to:
  return parent(part)

it fixes my problem because part has the function name as a string, instead of getting it from obj, which returns '<lambda>' in my case.
msg32398 - (view) Author: timmeh (tctimmeh) Date: 2007-06-25 21:57
Here is code that causes the problem:

import unittest
class Exmpl(unittest.TestCase):
  pass
Exmpl.testBug = lambda self: 1+2
if (__name__ == '__main__'):
  unittest.main()

Running "program.py" with no args is an error free run.  Running "program.py Exmpl.testBug" dies with the stack trace mentioned above.
msg85470 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2009-04-05 10:06
This is fixed in at least 2.6 and trunk.
History
Date User Action Args
2022-04-11 14:56:25adminsetgithub: 45129
2009-04-05 10:06:07georg.brandlsetstatus: open -> closed

nosy: + georg.brandl
messages: + msg85470

resolution: out of date
2009-03-31 01:19:07ajaksu2setstage: test needed
type: enhancement
versions: + Python 3.1, Python 2.7, - Python 2.5
2007-06-25 16:28:18tctimmehcreate