Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: don't know how to make test from: None #37

Closed
dhermyt opened this issue Sep 18, 2016 · 13 comments
Closed

TypeError: don't know how to make test from: None #37

dhermyt opened this issue Sep 18, 2016 · 13 comments

Comments

@dhermyt
Copy link

dhermyt commented Sep 18, 2016

I cannot run sample test using Visual Studio 2015, Python 3.5 and unittest2:

import unittest
from nose_parameterized import parameterized

class AddTestCase(unittest.TestCase):
    @parameterized.expand([
        ("2 and 3", 2, 3, 5),
        ("3 and 5", 2, 3, 5),
    ])
    def test_add(self, _, a, b, expected):
        assert_equal(a + b, expected)
Test Name:  test_add
Test Outcome:   Failed
Result StandardError:   
Traceback (most recent call last):
  File "C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO 14.0\COMMON7\IDE\EXTENSIONS\MICROSOFT\PYTHON TOOLS FOR VISUAL STUDIO\2.2\visualstudio_py_testlauncher.py", line 69, in <module>
    main()
  File "C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO 14.0\COMMON7\IDE\EXTENSIONS\MICROSOFT\PYTHON TOOLS FOR VISUAL STUDIO\2.2\visualstudio_py_testlauncher.py", line 62, in main
    test = unittest.defaultTestLoader.loadTestsFromNames(opts.tests, module)
  File "C:\Users\lem1x\AppData\Local\Programs\Python\Python35-32\lib\unittest\loader.py", line 219, in loadTestsFromNames
    suites = [self.loadTestsFromName(name, module) for name in names]
  File "C:\Users\lem1x\AppData\Local\Programs\Python\Python35-32\lib\unittest\loader.py", line 219, in <listcomp>
    suites = [self.loadTestsFromName(name, module) for name in names]
  File "C:\Users\lem1x\AppData\Local\Programs\Python\Python35-32\lib\unittest\loader.py", line 213, in loadTestsFromName
    raise TypeError("don't know how to make test from: %s" % obj)
TypeError: don't know how to make test from: None
@wolever
Copy link
Owner

wolever commented Sep 18, 2016

That's very strange!

Does the test run as expected without the @parameterized decorator? And is there any chance you'd be able to recreate it from the command line? (I don't have a copy of Visual Studio around to test with)

@dhermyt
Copy link
Author

dhermyt commented Sep 18, 2016

Test works perfectly fine without @parametrized.
Unfortunately I'm new to python and have no idea how to do this from command line.
If you would give me more direct instructions then we can try to reproduce it.

@wolever
Copy link
Owner

wolever commented Sep 18, 2016

Okay, could you try:

  1. Add the line unittest.main() to the end of the file
  2. Run the file as if it were a Python file, not a test file (I don't know exactly how to do this with VS, but probably clicking a "run file" button?)

And see if it works or if it crashes.

When I run this test with Python 3.5 + unittest it seems to work, so I'd like to see if it's something strange with the Visual Studio test runner, or with my testing setup.

@dhermyt
Copy link
Author

dhermyt commented Sep 18, 2016

Ok, so my code looks like this:

import unittest
from nose_parameterized import parameterized

class AddTestCase(unittest.TestCase):
    @parameterized.expand([
        ("2 and 3", 2, 3, 5),
        ("3 and 5", 2, 3, 5),
    ])
    def test_add(self, _, a, b, expected):
        self.assertEqual(a + b, expected)

unittest.main()

When I select this file and run directly from VS it seems to be working fine:

..
----------------------------------------------------------------------
Ran 2 tests in 0.001s

OK
Press any key to continue . . .

However now I get a different error with built-in test runner in VS:

Test Name:  test_add
Test Outcome:   Failed
Result StandardError:   
usage: visualstudio_py_testlauncher.py [-h] [-v] [-q] [--locals] [-f] [-c]
                                       [-b]
                                       [tests [tests ...]]
visualstudio_py_testlauncher.py: error: unrecognized arguments: -m -t AddTestCase.test_add

Thanks for responding at this late hour ; )

@wolever
Copy link
Owner

wolever commented Sep 18, 2016

Hmm okay, so it's definitely an issue with the VS launcher then… and unfortunately, without access to VS, I'm not sure how to debug that. Do you have a friend or colleague who may be able to help with the debugging?

@dhermyt
Copy link
Author

dhermyt commented Sep 19, 2016

I'm afraid I don't.
Maybe someone else will post the solution here.

@BojanKogoj
Copy link

I'm having the same issue. Only happens if I run a specific test, if test them all it works. Using flask, python 3.5

@wolever
Copy link
Owner

wolever commented Mar 21, 2017

Ah! Okay, in coming back I think I know what the issue is: it looks like VisualStudio is trying to run a test named AddTestCase.test_add, but it doesn't exist any more – @paramterized.expand removes the original test function after it's been expanded.

Unfortunately I can't think of any great way to handle this. A couple of less-great-but-would-work ways, though:

  • Use a different test runner (nose and py.test support "true" test generators).
  • Hack the visualstudio_py_testlauncher.py so it checks to see if the requested testcase is a parameterized case and expands the command line to include all the parameterized tests.
  • Does VisualStudio have an option to run a test class instead of a specific test case? If so, that could be a good option.

Please let me know if you come up with something!

@etaiklein
Copy link

I ran into this running tests in pycharm recently, the fix was to run unit tests on the whole file rather than the just the parameterized test case.

@wolever wolever closed this as completed Feb 6, 2019
@asmodehn
Copy link

Just had this problem (MS VS et unittest - only test runner supported), and I was thinking about this potential solution :

it looks like VisualStudio is trying to run a test named AddTestCase.test_add, but it doesn't exist any more – @paramterized.expand removes the original test function after it's been expanded.

How about making @parameterized.expand() :

I am making the assumption that people using unittest as a test runner might want a minimum of dependencies and very simple/known/unittest like behavior.
This way it should not modify the user (tools and IDE) expectations too much...

@TurnUpTheMike
Copy link

What is happening is that when you run all of the tests, the parameterized.expand decorator is creating individual test cases for each of the items in the expand array. The individual testcase is named <your_test_name>_0 <your_test_name>_1 and so on.

You can run the individual test case now that you have the specific name of the test case. Append the _0 to run the first entry of your parameterized list.

If this doesn't make sense, you can prove this out by adding something that will purposefully fail the test self.assertTrue(False) and then running all of the tests. Your failures will say the exact test case that failed.

@evanli01
Copy link

raise TypeError("don't know how to make test from: %s" % obj)
TypeError: don't know how to make test from: None

i found the issue, the cursor leave the function, ight click the blank space and run the function is pass

光标或者鼠标不要这个参数化函数上,然后右击点运行,就可以啦~

dingo9 added a commit to dingo9/parameterized that referenced this issue Sep 7, 2021
add subtest for unittest
fix issue wolever#98 wolever#37
@Ronserruya
Copy link

Ronserruya commented Feb 20, 2022

Temp solution when running a single test directly from the Pycharm dropdown menu (where you can't specify the name)

class TestSomething(unittest.TestCase):

    @parameterized.expand([(1,2,3), (2,2,4)])
    def test_addition(self, a, b, c):
        assert a+b == c

    @parameterized.expand([('hello', 5), ('xx', 2)])
    def test_string_len(self, a, b):
        assert len(a) == b

TestSomething.test_addition = TestSomething.test_addition_0 
TestSomething.test_string_len = TestSomething.test_string_len_1_xx

Doing this you can run/debug the test you chose, in this case, the first set of params for test_addition, or the second set for test_string_len
Screen Shot 2022-02-20 at 15 38 31

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants