Get pytest autocompletion in zshell





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















This question may be better suited for superuser -- if that's the case let me know and I'll shift it.



I use zsh and frequently run pytest from the command line. A very common situation is that I need to run a specific test (or a subtest of a class).



The former looks something like



pytest test/test_foo_file.py::test_foo_function



and the latter something like



pytest test/test_foo_file.py::FooClassTest::test_specific_functionality



It's kind of a pain to write out the entire exact class and test name, and this seems like something ripe for autocompletion or fuzzysearching of some kind. I've been unable to achieve this with what I've found researching -- does anyone have any recommendations?



Let me know if I can be more specific in any way.










share|improve this question





























    1















    This question may be better suited for superuser -- if that's the case let me know and I'll shift it.



    I use zsh and frequently run pytest from the command line. A very common situation is that I need to run a specific test (or a subtest of a class).



    The former looks something like



    pytest test/test_foo_file.py::test_foo_function



    and the latter something like



    pytest test/test_foo_file.py::FooClassTest::test_specific_functionality



    It's kind of a pain to write out the entire exact class and test name, and this seems like something ripe for autocompletion or fuzzysearching of some kind. I've been unable to achieve this with what I've found researching -- does anyone have any recommendations?



    Let me know if I can be more specific in any way.










    share|improve this question

























      1












      1








      1








      This question may be better suited for superuser -- if that's the case let me know and I'll shift it.



      I use zsh and frequently run pytest from the command line. A very common situation is that I need to run a specific test (or a subtest of a class).



      The former looks something like



      pytest test/test_foo_file.py::test_foo_function



      and the latter something like



      pytest test/test_foo_file.py::FooClassTest::test_specific_functionality



      It's kind of a pain to write out the entire exact class and test name, and this seems like something ripe for autocompletion or fuzzysearching of some kind. I've been unable to achieve this with what I've found researching -- does anyone have any recommendations?



      Let me know if I can be more specific in any way.










      share|improve this question














      This question may be better suited for superuser -- if that's the case let me know and I'll shift it.



      I use zsh and frequently run pytest from the command line. A very common situation is that I need to run a specific test (or a subtest of a class).



      The former looks something like



      pytest test/test_foo_file.py::test_foo_function



      and the latter something like



      pytest test/test_foo_file.py::FooClassTest::test_specific_functionality



      It's kind of a pain to write out the entire exact class and test name, and this seems like something ripe for autocompletion or fuzzysearching of some kind. I've been unable to achieve this with what I've found researching -- does anyone have any recommendations?



      Let me know if I can be more specific in any way.







      python autocomplete zsh pytest






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 4 at 18:33









      Peter DolanPeter Dolan

      884518




      884518
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Disclaimer: I am not a zsh user, but the approach is pretty much similar to the customizing of bash completions:





          1. Create a custom completion file, e.g.



            $ mkdir ~/.zsh-completions
            $ touch ~/.zsh-completions/_pytest



          2. Inside ~/.zsh-completions/_pytest, write the completion function:



            #compdef pytest

            _pytest_complete() {
            local curcontext="$curcontext" state line
            typeset -A opt_args
            compadd "$@" $( pytest --collect-only -q | head -n -2)
            }

            _pytest_complete "$@"



          3. Adjust .zshrc to include custom completions, e.g.



            fpath=(~/.zsh-completions $fpath)
            autoload -U compinit
            compinit
            zstyle ':completion:*' menu select=2



          Restart the shell. Now you should get the single tests selection on tab completion:



          enter image description here



          The crucial command here is



          $ pytest --collect-only -q | head -n -2


          which collects the tests in current directory and lists their names ready to be passed as command line arguments.






          share|improve this answer
























          • Thank you so much for this! Im going to try it later (currently afk) and will mark as correct assuming it works :)

            – Peter Dolan
            Jan 5 at 3:13











          • Hey @hoefling, thanks again. This largely worked, so I'm going to mark as correct. One minor issue -- it's very slow. It takes ~5-10 seconds to load the menu of possible tests -- is this something you've experienced? Thanks again

            – Peter Dolan
            Jan 5 at 9:44













          • Yeah, depending on how many tests you have, test collection can get very slow. If the collect command from above is slow when you run it explicitly (e.g. time pytest --collect-only -q), then the completions will also be slow.

            – hoefling
            Jan 5 at 12:02











          • Damn, got it. Maybe I can somehow cache and pre optimize. Thank you!!

            – Peter Dolan
            Jan 5 at 19:34












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54044327%2fget-pytest-autocompletion-in-zshell%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Disclaimer: I am not a zsh user, but the approach is pretty much similar to the customizing of bash completions:





          1. Create a custom completion file, e.g.



            $ mkdir ~/.zsh-completions
            $ touch ~/.zsh-completions/_pytest



          2. Inside ~/.zsh-completions/_pytest, write the completion function:



            #compdef pytest

            _pytest_complete() {
            local curcontext="$curcontext" state line
            typeset -A opt_args
            compadd "$@" $( pytest --collect-only -q | head -n -2)
            }

            _pytest_complete "$@"



          3. Adjust .zshrc to include custom completions, e.g.



            fpath=(~/.zsh-completions $fpath)
            autoload -U compinit
            compinit
            zstyle ':completion:*' menu select=2



          Restart the shell. Now you should get the single tests selection on tab completion:



          enter image description here



          The crucial command here is



          $ pytest --collect-only -q | head -n -2


          which collects the tests in current directory and lists their names ready to be passed as command line arguments.






          share|improve this answer
























          • Thank you so much for this! Im going to try it later (currently afk) and will mark as correct assuming it works :)

            – Peter Dolan
            Jan 5 at 3:13











          • Hey @hoefling, thanks again. This largely worked, so I'm going to mark as correct. One minor issue -- it's very slow. It takes ~5-10 seconds to load the menu of possible tests -- is this something you've experienced? Thanks again

            – Peter Dolan
            Jan 5 at 9:44













          • Yeah, depending on how many tests you have, test collection can get very slow. If the collect command from above is slow when you run it explicitly (e.g. time pytest --collect-only -q), then the completions will also be slow.

            – hoefling
            Jan 5 at 12:02











          • Damn, got it. Maybe I can somehow cache and pre optimize. Thank you!!

            – Peter Dolan
            Jan 5 at 19:34
















          1














          Disclaimer: I am not a zsh user, but the approach is pretty much similar to the customizing of bash completions:





          1. Create a custom completion file, e.g.



            $ mkdir ~/.zsh-completions
            $ touch ~/.zsh-completions/_pytest



          2. Inside ~/.zsh-completions/_pytest, write the completion function:



            #compdef pytest

            _pytest_complete() {
            local curcontext="$curcontext" state line
            typeset -A opt_args
            compadd "$@" $( pytest --collect-only -q | head -n -2)
            }

            _pytest_complete "$@"



          3. Adjust .zshrc to include custom completions, e.g.



            fpath=(~/.zsh-completions $fpath)
            autoload -U compinit
            compinit
            zstyle ':completion:*' menu select=2



          Restart the shell. Now you should get the single tests selection on tab completion:



          enter image description here



          The crucial command here is



          $ pytest --collect-only -q | head -n -2


          which collects the tests in current directory and lists their names ready to be passed as command line arguments.






          share|improve this answer
























          • Thank you so much for this! Im going to try it later (currently afk) and will mark as correct assuming it works :)

            – Peter Dolan
            Jan 5 at 3:13











          • Hey @hoefling, thanks again. This largely worked, so I'm going to mark as correct. One minor issue -- it's very slow. It takes ~5-10 seconds to load the menu of possible tests -- is this something you've experienced? Thanks again

            – Peter Dolan
            Jan 5 at 9:44













          • Yeah, depending on how many tests you have, test collection can get very slow. If the collect command from above is slow when you run it explicitly (e.g. time pytest --collect-only -q), then the completions will also be slow.

            – hoefling
            Jan 5 at 12:02











          • Damn, got it. Maybe I can somehow cache and pre optimize. Thank you!!

            – Peter Dolan
            Jan 5 at 19:34














          1












          1








          1







          Disclaimer: I am not a zsh user, but the approach is pretty much similar to the customizing of bash completions:





          1. Create a custom completion file, e.g.



            $ mkdir ~/.zsh-completions
            $ touch ~/.zsh-completions/_pytest



          2. Inside ~/.zsh-completions/_pytest, write the completion function:



            #compdef pytest

            _pytest_complete() {
            local curcontext="$curcontext" state line
            typeset -A opt_args
            compadd "$@" $( pytest --collect-only -q | head -n -2)
            }

            _pytest_complete "$@"



          3. Adjust .zshrc to include custom completions, e.g.



            fpath=(~/.zsh-completions $fpath)
            autoload -U compinit
            compinit
            zstyle ':completion:*' menu select=2



          Restart the shell. Now you should get the single tests selection on tab completion:



          enter image description here



          The crucial command here is



          $ pytest --collect-only -q | head -n -2


          which collects the tests in current directory and lists their names ready to be passed as command line arguments.






          share|improve this answer













          Disclaimer: I am not a zsh user, but the approach is pretty much similar to the customizing of bash completions:





          1. Create a custom completion file, e.g.



            $ mkdir ~/.zsh-completions
            $ touch ~/.zsh-completions/_pytest



          2. Inside ~/.zsh-completions/_pytest, write the completion function:



            #compdef pytest

            _pytest_complete() {
            local curcontext="$curcontext" state line
            typeset -A opt_args
            compadd "$@" $( pytest --collect-only -q | head -n -2)
            }

            _pytest_complete "$@"



          3. Adjust .zshrc to include custom completions, e.g.



            fpath=(~/.zsh-completions $fpath)
            autoload -U compinit
            compinit
            zstyle ':completion:*' menu select=2



          Restart the shell. Now you should get the single tests selection on tab completion:



          enter image description here



          The crucial command here is



          $ pytest --collect-only -q | head -n -2


          which collects the tests in current directory and lists their names ready to be passed as command line arguments.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 5 at 1:20









          hoeflinghoefling

          14.1k43768




          14.1k43768













          • Thank you so much for this! Im going to try it later (currently afk) and will mark as correct assuming it works :)

            – Peter Dolan
            Jan 5 at 3:13











          • Hey @hoefling, thanks again. This largely worked, so I'm going to mark as correct. One minor issue -- it's very slow. It takes ~5-10 seconds to load the menu of possible tests -- is this something you've experienced? Thanks again

            – Peter Dolan
            Jan 5 at 9:44













          • Yeah, depending on how many tests you have, test collection can get very slow. If the collect command from above is slow when you run it explicitly (e.g. time pytest --collect-only -q), then the completions will also be slow.

            – hoefling
            Jan 5 at 12:02











          • Damn, got it. Maybe I can somehow cache and pre optimize. Thank you!!

            – Peter Dolan
            Jan 5 at 19:34



















          • Thank you so much for this! Im going to try it later (currently afk) and will mark as correct assuming it works :)

            – Peter Dolan
            Jan 5 at 3:13











          • Hey @hoefling, thanks again. This largely worked, so I'm going to mark as correct. One minor issue -- it's very slow. It takes ~5-10 seconds to load the menu of possible tests -- is this something you've experienced? Thanks again

            – Peter Dolan
            Jan 5 at 9:44













          • Yeah, depending on how many tests you have, test collection can get very slow. If the collect command from above is slow when you run it explicitly (e.g. time pytest --collect-only -q), then the completions will also be slow.

            – hoefling
            Jan 5 at 12:02











          • Damn, got it. Maybe I can somehow cache and pre optimize. Thank you!!

            – Peter Dolan
            Jan 5 at 19:34

















          Thank you so much for this! Im going to try it later (currently afk) and will mark as correct assuming it works :)

          – Peter Dolan
          Jan 5 at 3:13





          Thank you so much for this! Im going to try it later (currently afk) and will mark as correct assuming it works :)

          – Peter Dolan
          Jan 5 at 3:13













          Hey @hoefling, thanks again. This largely worked, so I'm going to mark as correct. One minor issue -- it's very slow. It takes ~5-10 seconds to load the menu of possible tests -- is this something you've experienced? Thanks again

          – Peter Dolan
          Jan 5 at 9:44







          Hey @hoefling, thanks again. This largely worked, so I'm going to mark as correct. One minor issue -- it's very slow. It takes ~5-10 seconds to load the menu of possible tests -- is this something you've experienced? Thanks again

          – Peter Dolan
          Jan 5 at 9:44















          Yeah, depending on how many tests you have, test collection can get very slow. If the collect command from above is slow when you run it explicitly (e.g. time pytest --collect-only -q), then the completions will also be slow.

          – hoefling
          Jan 5 at 12:02





          Yeah, depending on how many tests you have, test collection can get very slow. If the collect command from above is slow when you run it explicitly (e.g. time pytest --collect-only -q), then the completions will also be slow.

          – hoefling
          Jan 5 at 12:02













          Damn, got it. Maybe I can somehow cache and pre optimize. Thank you!!

          – Peter Dolan
          Jan 5 at 19:34





          Damn, got it. Maybe I can somehow cache and pre optimize. Thank you!!

          – Peter Dolan
          Jan 5 at 19:34




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54044327%2fget-pytest-autocompletion-in-zshell%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas