nvim-neotest / neotest-python

MIT License
116 stars 34 forks source link

django `python manage.py test` support? #51

Closed hariom-qure closed 7 months ago

hariom-qure commented 12 months ago

I tried finding this a lot but didn't have any luck. Is it possible to run django tests?
How do I configure it? Currently, simply running with unittest runner gives settings improperly configured error

afrischk commented 12 months ago

Same for me. Any advice?

afrischk commented 12 months ago

Ok. Simple workaround: export DJANGO_SETTINGS_MODULE=yoursettings Maybe you could also create an .env file with the env-variable.

rcarriga commented 12 months ago

There is no django support right now and I don't use it so I won't be adding it. Open to contributions though :smile:

afrischk commented 12 months ago

Can you give me a hint what needs to be done? I think the only difference to the python adapter is to call python manage.py test instead of python -m unittest. Do I need to implement a new test adapter like neotest-django? :-)

danielebra commented 11 months ago

Can you give me a hint what needs to be done? I think the only difference to the python adapter is to call python manage.py test instead of python -m unittest. Do I need to implement a new test adapter like neotest-django? :-)

Hey @afrischk ,

Did you find out what the next steps would be to perform the transformation of the test entry point? Would be keen on adding support for pants

This is what I had in an old config file that was used with ultest I believe

let test#python#runner='pytest'
let test#python#pytest#executable='pants --no-process-cleanup test'

function! PantsTransform(cmd) abort
  let single_test_cmd = substitute(a:cmd[-1], '::', ' -- -k ', '')
  let a:cmd[-1] = single_test_cmd
  return a:cmd
endfunction

let g:test#custom_transformations = {'pants': function('PantsTransform')}
let g:test#transformation = 'pants'

This in essence just performs the transformation on the entry point


From a brief look at the code in this repo, the entry point for the existing adapters are imported libraries eg

result_collector = NeotestResultCollector(self, stream=stream)
pytest.main(args=args, plugins=[
    result_collector,
    NeotestDebugpyPlugin(),
])
return result_collector.results
unittest.main(
    module=None,
    argv=argv,
    testRunner=NeotestUnittestRunner(resultclass=NeotestTextTestResult),
    exit=False,
)
afrischk commented 11 months ago

Hi @danielebra,

to be honest I had not much time to look into this. I think for Django I have to implement a NeotestUnitestRunner that inherits from the DiscoverRunner instead of the TextTestRunner. Also I think the code from the NeotestUnitestRunner can stay almost the same.

For pytest I have not looked into the code too carefully, but I think you look at the right places. You somehow need to tell pytest to use pants and maybe adapt the NeotestResultCollector? Sorry for being so vague. New insights much appreciated :-)

danielebra commented 11 months ago

Thanks @afrischk

I put together a very quick and dirty bandaid to just be able to invoke pants

https://github.com/danielebra/neotest-python/commit/6f830f84e43bcb6c2f48b135f6a85a25e93961ca

afrischk commented 11 months ago

Uh nice. Maybe I can do the same for django without using the DiscoverRunner.

afrischk commented 11 months ago

@danielebra Can you give me a hint on how to debug python plugins in neovim? I am new to plugin development...

I know there is nvim-dap, can I use it here somehow?

danielebra commented 11 months ago

@afrischk this was my first time editing a plug-in too. I cloned the directory and pointed my packer plug-in to the path of the local plugin and then installed it. This provided real-time feedback to changes.

I used the neotest output panel to see errors and feedback as I made changes. When using print in the plugin, I saw that feedback surfaced in the neotest floating window and output panel.

Also running the :messages command in nvim gave some info

I personally didn't try using dap on the plug-in, but I did invoke it a few times briefly in my target application where I was running the python tests and I think it intercepted the code at the neotest level, so that could be useful too

afrischk commented 10 months ago

@danielebra Nice, thanks!

@rcarriga I have created a PR for django support -> #54

sampiecz commented 8 months ago

Thank you for creating that branch / PR @afrischk :muscle:

I installed neotest-python from your repo with the django-support branch successfully like so:

{
    'nvim-neotest/neotest',
    dependencies = {
      "nvim-lua/plenary.nvim",
      "nvim-treesitter/nvim-treesitter",
      "antoinemadec/FixCursorHold.nvim",
      { "afrischk/neotest-python", branch = "django-support" },
    },
    config = function()
      require('neotest').setup({
        adapters = {
          require("neotest-python")({
            env = { DJANGO_SETTINGS_MODULE = "AppName.settings" },
            dap = { justMyCode = false },
            args = { "--log-level", "DEBUG" },
            runner = "django",
          }),
        }
      })
    end
  }

FWIW I'm using lunarvim and lazy.nvim to test this out.

That being said, I keep getting no tests found, but didn't have that issue with pytest in the past.

It's probably on my end, but wanted to ask in case it was useful QA / testing.

Let me know if there is a better place to discuss, figured here may be less noisy than on the PR.

afrischk commented 8 months ago

Hi @sampiecz 👋

thanks for reporting!

I currently have the feeling django rather has trouble detecting the tests than there is an issue with the plugin. Do you have a minimal example project so that I am able to reproduce?

ikonitas commented 8 months ago

Hi @sampiecz 👋

thanks for reporting!

I currently have the feeling django rather has trouble detecting the tests than there is an issue with the plugin. Do you have a minimal example project so that I am able to reproduce?

Great work! I can confirm that this is working, but the only issue I am having to pass argument like --keepdb

afrischk commented 8 months ago

Hi @sampiecz 👋

thanks for reporting!

I currently have the feeling django rather has trouble detecting the tests than there is an issue with the plugin. Do you have a minimal example project so that I am able to reproduce?

Great work! I can confirm that this is working, but the only issue I am having to pass argument like --keepdb

Thanks! Yah, i just expose resultclass and verbosity at the moment.

runner = DjangoUnittestRunner(resultclass=NeotestTextTestResult, verbosity=2)
sampiecz commented 8 months ago

Hi @sampiecz 👋

thanks for reporting!

I currently have the feeling django rather has trouble detecting the tests than there is an issue with the plugin. Do you have a minimal example project so that I am able to reproduce?

Thank you @afrischk it's working on my end now too. Was simply due to test naming / file naming conventions.

sampiecz commented 8 months ago

Great work! I can confirm that this is working, but the only issue I am having to pass argument like --keepdb

+1 to the passing --keepdb, I haven't figured out how to do that, so I attach to the test and manually hit yes/no.

I tried to grok the runner = ... comment but wasn't sure how to modify my config.lua to use DjangoUnittestRunner & NeotestTextTestResult.

afrischk commented 7 months ago

@sampiecz @ikonitas I have updated the PR. All django test args should be supported now. Can you please check?

args = {"--keepdb"} is the way to go now.

afrischk commented 7 months ago

54 is merged to master now

sampiecz commented 7 months ago

Thank you so much @afrischk , that works!

So. Much. Faster.

Really appreciate you taking the time to make & merge this PR :bow:

kenunq commented 2 months ago

https://github.com/kenunq/django-neotest

improved version, auto detect DJANGO_SETTINGS_MODULE, the names of the test files have been expanded