rcarriga / vim-ultest

The ultimate testing plugin for (Neo)Vim
MIT License
385 stars 15 forks source link

Test in camelcase won't run #39

Closed seblj closed 3 years ago

seblj commented 3 years ago

Describe the bug Test won't run if name of test is in camelcase.

18:57:48 | INFO | MainThread | logging.py:create_logger:98 | Logger created
18:57:48 | DEBUG | MainThread | __init__.py:__init__:41 | Handler created
18:57:48 | DEBUG | Thread-1 | __init__.py:_handle_coroutine:57 | Starting job with group update_positions
18:57:48 | INFO | Thread-1 | tracker.py:_async_update:52 | Updating positions in test_token.py
18:57:48 | DEBUG | Thread-1 | file.py:parse_file_structure:22 | Converted pattern {'test': ['\\v^\\s*%(async )?def (test_\\w+)'], 'namespace': ['\\v^\\s*class (\\w+)']} to {'test': [re.compile('^\\s*(?:async )?def (test_\\w+)')], 'namespace': [re.compile('^\\s*class (\\w+)')]}
18:57:48 | DEBUG | Thread-1 | tracker.py:_async_update:79 | New test test_token.py found in test_token.py
18:57:48 | DEBUG | Thread-1 | tracker.py:_remove_old_positions:122 | No tests removed
18:57:48 | DEBUG | Thread-1 | __init__.py:_handle_coroutine:77 | Finished job with group update_positions
18:57:52 | INFO | MainThread | __init__.py:run_nearest:125 | Running nearest test in test_token.py at line 0
18:57:58 | INFO | MainThread | __init__.py:run_nearest:125 | Running nearest test in test_token.py at line 1

I am using djangotest as the python runner

To Reproduce Create a test, and name it with camelcase in a django project, and try to run it. For example: This won't run.

def testSomething:
    self.assertEqual(True, True)

This will run

def test_something:
    self.assertEqual(True, True)

Expected behavior As it runs as expected with vim-test, I would expect it to be run with vim-ultest as well.

Also while I am here, I have a simple question as well: Is it possible to clear the icons after running tests? Couldn't figure out how to do this

rcarriga commented 3 years ago

So I can't see how the camel case works with vim-test. The patterns used to find the test in vim-test (same in vim-ultest) is '\v^\s*%(async )?def (test_\w+)' which wouldn't allow for camel case (test_\w+). I've even looked at the code for django specifically and it is doing no sort of case conversion.

What I think is happening is that you are running the TestNearest command which is picking up the test's parent class and running that instead. I'm not familiar at all with django so can't set up a project quickly, Could you create a test file with two camel case tests to see if they will run separately? Something like

from django.test import TestCase
class TestUltest(TestCase):

    def testA(self):
        assert False

    def testB(self):
        assert False

and tell me if you see both failed tests in the output of TestNearest

To your second question, there's no functionality to do that right, though I could certainly add it. Could you tell me what is the reason you'd like that?

seblj commented 3 years ago

If I run TestNearest in the test-file you mentioned, it ran both tests. None of the test were run if I tried to use UltestNearest or Ultest.

I have no reason to clear the icons other than I think it would look better if I am writing some more tests. Just personal preference really

rcarriga commented 3 years ago

OK so that confirms it. The tests are not running individually... This is an issue with vim-test then. vim-ultest can't run the tests individually if vim-test can't.

There's a few different avenues you can go down, since there's nothing I can do can change the behaviour of vim-test I'm afraid:

  1. Change the patterns used by vim-test to detect python tests. This would be very easy except that vim-test doesn't check if the variable for python patterns already exists so you'll need to set it after loading vim-test. All you need to set is like this (just removes the underscore after test:
    
    let test#python#patterns = {
    \ 'test':      ['\v^\s*%(async )?def (test\w+)'],
    \ 'namespace': ['\v^\s*class (\w+)'],
    \}
2. Open a PR to vim-test to either suggest the change above or the following to check the patterns don't exist so you can override in your own config with the above. This is easily done with the following code in `autoload/test/python.vim`
```vim
if !exists('g:test#python#patterns')
  let test#python#patterns = {
    \ 'test':      ['\v^\s*%(async )?def (test_\w+)'],
    \ 'namespace': ['\v^\s*class (\w+)'],
  \}
endif
  1. Change your test names to snake case because camel case in python is weird! (Not really very serious about this, name your functions whatever the hell you want 😅)
seblj commented 3 years ago

Okay thank you for checking this! I agree that I really should not use camel case, but in my defence, it was not me who named the test😂

Btw, could you add an option to clear the icons? Would really appreciate it!

rcarriga commented 3 years ago

Haha that's very fair, we all have to live with some sketchy code bases!

And yep can do, I will close this issue when it's in

rcarriga commented 3 years ago

Added a new command UltestClear which will wipe all results from the current file.

seblj commented 3 years ago

Works great! Thanks!