How to use unittest.TestSuite in VS Code?












0















In the future, I'll need to add many identical tests with different parameters. Now I am making a sample test suite:



import unittest

class TestCase(unittest.TestCase):
def __init__(self, methodName='runTest', param=None):
super(TestCase, self).__init__(methodName)
self.param = param

def test_something(self):
print 'n>>>>>> test_something: param =', self.param
self.assertEqual(1, 1)

if __name__ == "__main__":
suite = unittest.TestSuite()
testloader = unittest.TestLoader()
testnames = testloader.getTestCaseNames(TestCase)
for name in testnames:
suite.addTest(TestCase(name, param=42))
unittest.TextTestRunner(verbosity=2).run(suite)


It gets discovered by VS Code:



start
test.test_navigator.TestCase.test_something


When I run the tests, I don't receive the parameter:



test_something (test.test_navigator.TestCase) ... 
>>>>>> test_something: param = None
ok

----------------------------------------------------------------------
Ran 1 test in 0.001s

OK


If I run this file directly, everything works as expected (note param = 42 part)



test_something (__main__.TestCase) ...
>>>>>> test_something: param = 42
ok

----------------------------------------------------------------------
Ran 1 test in 0.001s

OK


So it looks like VS Code is running the tests on its own just by using the discovered classes and ignoring TestSuite completely?



What am I doing wrong?



Thanks.










share|improve this question



























    0















    In the future, I'll need to add many identical tests with different parameters. Now I am making a sample test suite:



    import unittest

    class TestCase(unittest.TestCase):
    def __init__(self, methodName='runTest', param=None):
    super(TestCase, self).__init__(methodName)
    self.param = param

    def test_something(self):
    print 'n>>>>>> test_something: param =', self.param
    self.assertEqual(1, 1)

    if __name__ == "__main__":
    suite = unittest.TestSuite()
    testloader = unittest.TestLoader()
    testnames = testloader.getTestCaseNames(TestCase)
    for name in testnames:
    suite.addTest(TestCase(name, param=42))
    unittest.TextTestRunner(verbosity=2).run(suite)


    It gets discovered by VS Code:



    start
    test.test_navigator.TestCase.test_something


    When I run the tests, I don't receive the parameter:



    test_something (test.test_navigator.TestCase) ... 
    >>>>>> test_something: param = None
    ok

    ----------------------------------------------------------------------
    Ran 1 test in 0.001s

    OK


    If I run this file directly, everything works as expected (note param = 42 part)



    test_something (__main__.TestCase) ...
    >>>>>> test_something: param = 42
    ok

    ----------------------------------------------------------------------
    Ran 1 test in 0.001s

    OK


    So it looks like VS Code is running the tests on its own just by using the discovered classes and ignoring TestSuite completely?



    What am I doing wrong?



    Thanks.










    share|improve this question

























      0












      0








      0








      In the future, I'll need to add many identical tests with different parameters. Now I am making a sample test suite:



      import unittest

      class TestCase(unittest.TestCase):
      def __init__(self, methodName='runTest', param=None):
      super(TestCase, self).__init__(methodName)
      self.param = param

      def test_something(self):
      print 'n>>>>>> test_something: param =', self.param
      self.assertEqual(1, 1)

      if __name__ == "__main__":
      suite = unittest.TestSuite()
      testloader = unittest.TestLoader()
      testnames = testloader.getTestCaseNames(TestCase)
      for name in testnames:
      suite.addTest(TestCase(name, param=42))
      unittest.TextTestRunner(verbosity=2).run(suite)


      It gets discovered by VS Code:



      start
      test.test_navigator.TestCase.test_something


      When I run the tests, I don't receive the parameter:



      test_something (test.test_navigator.TestCase) ... 
      >>>>>> test_something: param = None
      ok

      ----------------------------------------------------------------------
      Ran 1 test in 0.001s

      OK


      If I run this file directly, everything works as expected (note param = 42 part)



      test_something (__main__.TestCase) ...
      >>>>>> test_something: param = 42
      ok

      ----------------------------------------------------------------------
      Ran 1 test in 0.001s

      OK


      So it looks like VS Code is running the tests on its own just by using the discovered classes and ignoring TestSuite completely?



      What am I doing wrong?



      Thanks.










      share|improve this question














      In the future, I'll need to add many identical tests with different parameters. Now I am making a sample test suite:



      import unittest

      class TestCase(unittest.TestCase):
      def __init__(self, methodName='runTest', param=None):
      super(TestCase, self).__init__(methodName)
      self.param = param

      def test_something(self):
      print 'n>>>>>> test_something: param =', self.param
      self.assertEqual(1, 1)

      if __name__ == "__main__":
      suite = unittest.TestSuite()
      testloader = unittest.TestLoader()
      testnames = testloader.getTestCaseNames(TestCase)
      for name in testnames:
      suite.addTest(TestCase(name, param=42))
      unittest.TextTestRunner(verbosity=2).run(suite)


      It gets discovered by VS Code:



      start
      test.test_navigator.TestCase.test_something


      When I run the tests, I don't receive the parameter:



      test_something (test.test_navigator.TestCase) ... 
      >>>>>> test_something: param = None
      ok

      ----------------------------------------------------------------------
      Ran 1 test in 0.001s

      OK


      If I run this file directly, everything works as expected (note param = 42 part)



      test_something (__main__.TestCase) ...
      >>>>>> test_something: param = 42
      ok

      ----------------------------------------------------------------------
      Ran 1 test in 0.001s

      OK


      So it looks like VS Code is running the tests on its own just by using the discovered classes and ignoring TestSuite completely?



      What am I doing wrong?



      Thanks.







      python visual-studio-code python-unittest






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 '18 at 18:25









      ababakababak

      443210




      443210
























          2 Answers
          2






          active

          oldest

          votes


















          1














          The problem is your code is in a if __name__ == "__main__" block which is only executed when you point Python directly at the file. So when the extension asks unittest to get all the tests and then run them for us it doesn't run the code in your if __name__ == "__main__" block (which is why it can find it but it doesn't do anything magical).



          If you can get it to work using unittest's command-line interface then the extension should run it as you want it to.






          share|improve this answer
























          • If I remove the if __name__ == "__main__" condition then the tests are executed two times: one time in a suite as expected and one time as individual tests. Also they are executed on discovery.

            – ababak
            Nov 21 '18 at 21:00













          • So while playing with unittest's CLI I realized that it's not a VS Code problem but the unittest's itself. I am getting the same behavior from CLI. I can't get the tests executed in a suite without getting them executed as standalone.

            – ababak
            Nov 21 '18 at 21:07



















          0














          The key is to implement the load_tests function:



          def load_tests(loader, tests, pattern):
          suite = unittest.TestSuite()
          testnames = loader.getTestCaseNames(TestCase)
          for name in testnames:
          suite.addTest(TestCase(name, param=42))
          suite.addTest(TestCase(name, param=84))
          return suite


          The documentation says:




          If load_tests exists then discovery does not recurse into the package, load_tests is responsible for loading all tests in the package.




          Now my tests run as expected.



          P.S. Thanks to Brett Cannon for pointing me to Unit testing framework documentation






          share|improve this answer























            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%2f53399258%2fhow-to-use-unittest-testsuite-in-vs-code%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            The problem is your code is in a if __name__ == "__main__" block which is only executed when you point Python directly at the file. So when the extension asks unittest to get all the tests and then run them for us it doesn't run the code in your if __name__ == "__main__" block (which is why it can find it but it doesn't do anything magical).



            If you can get it to work using unittest's command-line interface then the extension should run it as you want it to.






            share|improve this answer
























            • If I remove the if __name__ == "__main__" condition then the tests are executed two times: one time in a suite as expected and one time as individual tests. Also they are executed on discovery.

              – ababak
              Nov 21 '18 at 21:00













            • So while playing with unittest's CLI I realized that it's not a VS Code problem but the unittest's itself. I am getting the same behavior from CLI. I can't get the tests executed in a suite without getting them executed as standalone.

              – ababak
              Nov 21 '18 at 21:07
















            1














            The problem is your code is in a if __name__ == "__main__" block which is only executed when you point Python directly at the file. So when the extension asks unittest to get all the tests and then run them for us it doesn't run the code in your if __name__ == "__main__" block (which is why it can find it but it doesn't do anything magical).



            If you can get it to work using unittest's command-line interface then the extension should run it as you want it to.






            share|improve this answer
























            • If I remove the if __name__ == "__main__" condition then the tests are executed two times: one time in a suite as expected and one time as individual tests. Also they are executed on discovery.

              – ababak
              Nov 21 '18 at 21:00













            • So while playing with unittest's CLI I realized that it's not a VS Code problem but the unittest's itself. I am getting the same behavior from CLI. I can't get the tests executed in a suite without getting them executed as standalone.

              – ababak
              Nov 21 '18 at 21:07














            1












            1








            1







            The problem is your code is in a if __name__ == "__main__" block which is only executed when you point Python directly at the file. So when the extension asks unittest to get all the tests and then run them for us it doesn't run the code in your if __name__ == "__main__" block (which is why it can find it but it doesn't do anything magical).



            If you can get it to work using unittest's command-line interface then the extension should run it as you want it to.






            share|improve this answer













            The problem is your code is in a if __name__ == "__main__" block which is only executed when you point Python directly at the file. So when the extension asks unittest to get all the tests and then run them for us it doesn't run the code in your if __name__ == "__main__" block (which is why it can find it but it doesn't do anything magical).



            If you can get it to work using unittest's command-line interface then the extension should run it as you want it to.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 21 '18 at 20:54









            Brett CannonBrett Cannon

            1,7061129




            1,7061129













            • If I remove the if __name__ == "__main__" condition then the tests are executed two times: one time in a suite as expected and one time as individual tests. Also they are executed on discovery.

              – ababak
              Nov 21 '18 at 21:00













            • So while playing with unittest's CLI I realized that it's not a VS Code problem but the unittest's itself. I am getting the same behavior from CLI. I can't get the tests executed in a suite without getting them executed as standalone.

              – ababak
              Nov 21 '18 at 21:07



















            • If I remove the if __name__ == "__main__" condition then the tests are executed two times: one time in a suite as expected and one time as individual tests. Also they are executed on discovery.

              – ababak
              Nov 21 '18 at 21:00













            • So while playing with unittest's CLI I realized that it's not a VS Code problem but the unittest's itself. I am getting the same behavior from CLI. I can't get the tests executed in a suite without getting them executed as standalone.

              – ababak
              Nov 21 '18 at 21:07

















            If I remove the if __name__ == "__main__" condition then the tests are executed two times: one time in a suite as expected and one time as individual tests. Also they are executed on discovery.

            – ababak
            Nov 21 '18 at 21:00







            If I remove the if __name__ == "__main__" condition then the tests are executed two times: one time in a suite as expected and one time as individual tests. Also they are executed on discovery.

            – ababak
            Nov 21 '18 at 21:00















            So while playing with unittest's CLI I realized that it's not a VS Code problem but the unittest's itself. I am getting the same behavior from CLI. I can't get the tests executed in a suite without getting them executed as standalone.

            – ababak
            Nov 21 '18 at 21:07





            So while playing with unittest's CLI I realized that it's not a VS Code problem but the unittest's itself. I am getting the same behavior from CLI. I can't get the tests executed in a suite without getting them executed as standalone.

            – ababak
            Nov 21 '18 at 21:07













            0














            The key is to implement the load_tests function:



            def load_tests(loader, tests, pattern):
            suite = unittest.TestSuite()
            testnames = loader.getTestCaseNames(TestCase)
            for name in testnames:
            suite.addTest(TestCase(name, param=42))
            suite.addTest(TestCase(name, param=84))
            return suite


            The documentation says:




            If load_tests exists then discovery does not recurse into the package, load_tests is responsible for loading all tests in the package.




            Now my tests run as expected.



            P.S. Thanks to Brett Cannon for pointing me to Unit testing framework documentation






            share|improve this answer




























              0














              The key is to implement the load_tests function:



              def load_tests(loader, tests, pattern):
              suite = unittest.TestSuite()
              testnames = loader.getTestCaseNames(TestCase)
              for name in testnames:
              suite.addTest(TestCase(name, param=42))
              suite.addTest(TestCase(name, param=84))
              return suite


              The documentation says:




              If load_tests exists then discovery does not recurse into the package, load_tests is responsible for loading all tests in the package.




              Now my tests run as expected.



              P.S. Thanks to Brett Cannon for pointing me to Unit testing framework documentation






              share|improve this answer


























                0












                0








                0







                The key is to implement the load_tests function:



                def load_tests(loader, tests, pattern):
                suite = unittest.TestSuite()
                testnames = loader.getTestCaseNames(TestCase)
                for name in testnames:
                suite.addTest(TestCase(name, param=42))
                suite.addTest(TestCase(name, param=84))
                return suite


                The documentation says:




                If load_tests exists then discovery does not recurse into the package, load_tests is responsible for loading all tests in the package.




                Now my tests run as expected.



                P.S. Thanks to Brett Cannon for pointing me to Unit testing framework documentation






                share|improve this answer













                The key is to implement the load_tests function:



                def load_tests(loader, tests, pattern):
                suite = unittest.TestSuite()
                testnames = loader.getTestCaseNames(TestCase)
                for name in testnames:
                suite.addTest(TestCase(name, param=42))
                suite.addTest(TestCase(name, param=84))
                return suite


                The documentation says:




                If load_tests exists then discovery does not recurse into the package, load_tests is responsible for loading all tests in the package.




                Now my tests run as expected.



                P.S. Thanks to Brett Cannon for pointing me to Unit testing framework documentation







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 21 '18 at 21:40









                ababakababak

                443210




                443210






























                    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%2f53399258%2fhow-to-use-unittest-testsuite-in-vs-code%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

                    Costa Masnaga

                    Fotorealismo

                    Sidney Franklin