Python unittest - how to chose the url on which the tests are executed?









up vote
3
down vote

favorite
1












I am somewhat of a beginner in python, i am currently writing a suite of test cases with selenium webdriver using unittest; i have also found a lot of useful answers here, but it's time a ask my first question, i have struggled a lot with this and cannot find a proper answer, so any help is greatly appreciated:



For short, i have a suite of multiple tests cases, and in each case the first step is always ".get('Some URL')"; i have written these test cases for a single environment, but i would like to be able to select the URL on which all tests will be executed. In the example below i called the "access_url" method with a specific environment, but i need to do this for all of my scenarios at once, is it possible to do this from where i execute the .py file (e.g. "python example.py")? or to pass it in the .run() method when i select what suite to run?



import HTMLTestRunner
from selenium import webdriver
import unittest


This is a custom class used to create the 'access_url' method



def MyClass(object):
def __init__(self, driver):
self.driver = driver

def access_url(self, URL):
if URL == 'environment 1':
self.driver.get('https://www.google.com/')
elif URL == 'environment 2':
self.driver.get('https://example.com/')


In the classes i use to write test cases the first step is always 'access URL'



class TestScenario01(unittest.TestCase):

def setUp(self):
[...]

def test_01_access(self):
MyClass(self.driver).access_url(URL='environment 2')

def test_02(self):
[...]

def test_03(self):
[...]


In order to run the tests i place them all in a suite and use .run() on them



tc_scenario01 = unittest.TestLoader().loadTestsFromTestCase(TestScenario01)
test_suite = unittest.TestSuite([tc_scenario01])
HTMLReporterCustom.HTMLTestRunner().run(test_suite)


Finally, in order to execute the script i type the follwoing line in CMD: 'python example_file.py



As i mentioned above, all i want to do is to be able to somehow pass the URL one time to all test cases that call the "access_url()" method. Thanks!










share|improve this question

























    up vote
    3
    down vote

    favorite
    1












    I am somewhat of a beginner in python, i am currently writing a suite of test cases with selenium webdriver using unittest; i have also found a lot of useful answers here, but it's time a ask my first question, i have struggled a lot with this and cannot find a proper answer, so any help is greatly appreciated:



    For short, i have a suite of multiple tests cases, and in each case the first step is always ".get('Some URL')"; i have written these test cases for a single environment, but i would like to be able to select the URL on which all tests will be executed. In the example below i called the "access_url" method with a specific environment, but i need to do this for all of my scenarios at once, is it possible to do this from where i execute the .py file (e.g. "python example.py")? or to pass it in the .run() method when i select what suite to run?



    import HTMLTestRunner
    from selenium import webdriver
    import unittest


    This is a custom class used to create the 'access_url' method



    def MyClass(object):
    def __init__(self, driver):
    self.driver = driver

    def access_url(self, URL):
    if URL == 'environment 1':
    self.driver.get('https://www.google.com/')
    elif URL == 'environment 2':
    self.driver.get('https://example.com/')


    In the classes i use to write test cases the first step is always 'access URL'



    class TestScenario01(unittest.TestCase):

    def setUp(self):
    [...]

    def test_01_access(self):
    MyClass(self.driver).access_url(URL='environment 2')

    def test_02(self):
    [...]

    def test_03(self):
    [...]


    In order to run the tests i place them all in a suite and use .run() on them



    tc_scenario01 = unittest.TestLoader().loadTestsFromTestCase(TestScenario01)
    test_suite = unittest.TestSuite([tc_scenario01])
    HTMLReporterCustom.HTMLTestRunner().run(test_suite)


    Finally, in order to execute the script i type the follwoing line in CMD: 'python example_file.py



    As i mentioned above, all i want to do is to be able to somehow pass the URL one time to all test cases that call the "access_url()" method. Thanks!










    share|improve this question























      up vote
      3
      down vote

      favorite
      1









      up vote
      3
      down vote

      favorite
      1






      1





      I am somewhat of a beginner in python, i am currently writing a suite of test cases with selenium webdriver using unittest; i have also found a lot of useful answers here, but it's time a ask my first question, i have struggled a lot with this and cannot find a proper answer, so any help is greatly appreciated:



      For short, i have a suite of multiple tests cases, and in each case the first step is always ".get('Some URL')"; i have written these test cases for a single environment, but i would like to be able to select the URL on which all tests will be executed. In the example below i called the "access_url" method with a specific environment, but i need to do this for all of my scenarios at once, is it possible to do this from where i execute the .py file (e.g. "python example.py")? or to pass it in the .run() method when i select what suite to run?



      import HTMLTestRunner
      from selenium import webdriver
      import unittest


      This is a custom class used to create the 'access_url' method



      def MyClass(object):
      def __init__(self, driver):
      self.driver = driver

      def access_url(self, URL):
      if URL == 'environment 1':
      self.driver.get('https://www.google.com/')
      elif URL == 'environment 2':
      self.driver.get('https://example.com/')


      In the classes i use to write test cases the first step is always 'access URL'



      class TestScenario01(unittest.TestCase):

      def setUp(self):
      [...]

      def test_01_access(self):
      MyClass(self.driver).access_url(URL='environment 2')

      def test_02(self):
      [...]

      def test_03(self):
      [...]


      In order to run the tests i place them all in a suite and use .run() on them



      tc_scenario01 = unittest.TestLoader().loadTestsFromTestCase(TestScenario01)
      test_suite = unittest.TestSuite([tc_scenario01])
      HTMLReporterCustom.HTMLTestRunner().run(test_suite)


      Finally, in order to execute the script i type the follwoing line in CMD: 'python example_file.py



      As i mentioned above, all i want to do is to be able to somehow pass the URL one time to all test cases that call the "access_url()" method. Thanks!










      share|improve this question













      I am somewhat of a beginner in python, i am currently writing a suite of test cases with selenium webdriver using unittest; i have also found a lot of useful answers here, but it's time a ask my first question, i have struggled a lot with this and cannot find a proper answer, so any help is greatly appreciated:



      For short, i have a suite of multiple tests cases, and in each case the first step is always ".get('Some URL')"; i have written these test cases for a single environment, but i would like to be able to select the URL on which all tests will be executed. In the example below i called the "access_url" method with a specific environment, but i need to do this for all of my scenarios at once, is it possible to do this from where i execute the .py file (e.g. "python example.py")? or to pass it in the .run() method when i select what suite to run?



      import HTMLTestRunner
      from selenium import webdriver
      import unittest


      This is a custom class used to create the 'access_url' method



      def MyClass(object):
      def __init__(self, driver):
      self.driver = driver

      def access_url(self, URL):
      if URL == 'environment 1':
      self.driver.get('https://www.google.com/')
      elif URL == 'environment 2':
      self.driver.get('https://example.com/')


      In the classes i use to write test cases the first step is always 'access URL'



      class TestScenario01(unittest.TestCase):

      def setUp(self):
      [...]

      def test_01_access(self):
      MyClass(self.driver).access_url(URL='environment 2')

      def test_02(self):
      [...]

      def test_03(self):
      [...]


      In order to run the tests i place them all in a suite and use .run() on them



      tc_scenario01 = unittest.TestLoader().loadTestsFromTestCase(TestScenario01)
      test_suite = unittest.TestSuite([tc_scenario01])
      HTMLReporterCustom.HTMLTestRunner().run(test_suite)


      Finally, in order to execute the script i type the follwoing line in CMD: 'python example_file.py



      As i mentioned above, all i want to do is to be able to somehow pass the URL one time to all test cases that call the "access_url()" method. Thanks!







      python unit-testing selenium webdriver






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 20:41









      Red Alex

      182




      182






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          You can maintain environment properties in separate config file.



          config.py



          DEFAULT_ENVIRONMENT='environment1'

          URL =
          'environment1': 'https://www.google.com/',
          'environment2': 'https://example.com/'



          Your Class,



          from package import config

          def MyClass(object):
          def __init__(self, driver):
          self.driver = driver

          def access_url(self):
          self.driver.get(config.URL[config.DEFAULT_ENVIRONMENT])


          Then test class will be as expected,



          class TestScenario01(unittest.TestCase):

          def setUp(self):
          [...]

          def test_01_access(self):
          MyClass(self.driver).access_url()

          def test_02(self):
          [...]

          def test_03(self):
          [...]


          While running test you can change,



          main.py



          from package import config

          config.DEFAULT_ENVIRONMENT = 'enviroment2'
          tc_scenario01 = unittest.TestLoader().loadTestsFromTestCase(TestScenario01)
          test_suite = unittest.TestSuite([tc_scenario01])
          HTMLReporterCustom.HTMLTestRunner().run(test_suite)


          You can also pass the environment name while running python main.py.



          main.py



          if __name__ == '__main__':
          config.DEFAULT_ENVIRONMENT = sys.argv[1] if len(sys.argv) > 2 else 'dev'
          tc_scenario01 = unittest.TestLoader().loadTestsFromTestCase(TestScenario01)
          test_suite = unittest.TestSuite([tc_scenario01])
          HTMLReporterCustom.HTMLTestRunner().run(test_suite)





          share|improve this answer






















          • This is so amazingly elegant! i was looking into all kinds of complicated solutions, it honestly never crossed my mind to put it in a variable and change it just before the .run() method. I am still wrapping my mind around all the programming concepts, so not only did you help me, but i also learned something. Thank you so much!
            – Red Alex
            Nov 11 at 21:54










          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',
          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%2f53253024%2fpython-unittest-how-to-chose-the-url-on-which-the-tests-are-executed%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








          up vote
          0
          down vote



          accepted










          You can maintain environment properties in separate config file.



          config.py



          DEFAULT_ENVIRONMENT='environment1'

          URL =
          'environment1': 'https://www.google.com/',
          'environment2': 'https://example.com/'



          Your Class,



          from package import config

          def MyClass(object):
          def __init__(self, driver):
          self.driver = driver

          def access_url(self):
          self.driver.get(config.URL[config.DEFAULT_ENVIRONMENT])


          Then test class will be as expected,



          class TestScenario01(unittest.TestCase):

          def setUp(self):
          [...]

          def test_01_access(self):
          MyClass(self.driver).access_url()

          def test_02(self):
          [...]

          def test_03(self):
          [...]


          While running test you can change,



          main.py



          from package import config

          config.DEFAULT_ENVIRONMENT = 'enviroment2'
          tc_scenario01 = unittest.TestLoader().loadTestsFromTestCase(TestScenario01)
          test_suite = unittest.TestSuite([tc_scenario01])
          HTMLReporterCustom.HTMLTestRunner().run(test_suite)


          You can also pass the environment name while running python main.py.



          main.py



          if __name__ == '__main__':
          config.DEFAULT_ENVIRONMENT = sys.argv[1] if len(sys.argv) > 2 else 'dev'
          tc_scenario01 = unittest.TestLoader().loadTestsFromTestCase(TestScenario01)
          test_suite = unittest.TestSuite([tc_scenario01])
          HTMLReporterCustom.HTMLTestRunner().run(test_suite)





          share|improve this answer






















          • This is so amazingly elegant! i was looking into all kinds of complicated solutions, it honestly never crossed my mind to put it in a variable and change it just before the .run() method. I am still wrapping my mind around all the programming concepts, so not only did you help me, but i also learned something. Thank you so much!
            – Red Alex
            Nov 11 at 21:54














          up vote
          0
          down vote



          accepted










          You can maintain environment properties in separate config file.



          config.py



          DEFAULT_ENVIRONMENT='environment1'

          URL =
          'environment1': 'https://www.google.com/',
          'environment2': 'https://example.com/'



          Your Class,



          from package import config

          def MyClass(object):
          def __init__(self, driver):
          self.driver = driver

          def access_url(self):
          self.driver.get(config.URL[config.DEFAULT_ENVIRONMENT])


          Then test class will be as expected,



          class TestScenario01(unittest.TestCase):

          def setUp(self):
          [...]

          def test_01_access(self):
          MyClass(self.driver).access_url()

          def test_02(self):
          [...]

          def test_03(self):
          [...]


          While running test you can change,



          main.py



          from package import config

          config.DEFAULT_ENVIRONMENT = 'enviroment2'
          tc_scenario01 = unittest.TestLoader().loadTestsFromTestCase(TestScenario01)
          test_suite = unittest.TestSuite([tc_scenario01])
          HTMLReporterCustom.HTMLTestRunner().run(test_suite)


          You can also pass the environment name while running python main.py.



          main.py



          if __name__ == '__main__':
          config.DEFAULT_ENVIRONMENT = sys.argv[1] if len(sys.argv) > 2 else 'dev'
          tc_scenario01 = unittest.TestLoader().loadTestsFromTestCase(TestScenario01)
          test_suite = unittest.TestSuite([tc_scenario01])
          HTMLReporterCustom.HTMLTestRunner().run(test_suite)





          share|improve this answer






















          • This is so amazingly elegant! i was looking into all kinds of complicated solutions, it honestly never crossed my mind to put it in a variable and change it just before the .run() method. I am still wrapping my mind around all the programming concepts, so not only did you help me, but i also learned something. Thank you so much!
            – Red Alex
            Nov 11 at 21:54












          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          You can maintain environment properties in separate config file.



          config.py



          DEFAULT_ENVIRONMENT='environment1'

          URL =
          'environment1': 'https://www.google.com/',
          'environment2': 'https://example.com/'



          Your Class,



          from package import config

          def MyClass(object):
          def __init__(self, driver):
          self.driver = driver

          def access_url(self):
          self.driver.get(config.URL[config.DEFAULT_ENVIRONMENT])


          Then test class will be as expected,



          class TestScenario01(unittest.TestCase):

          def setUp(self):
          [...]

          def test_01_access(self):
          MyClass(self.driver).access_url()

          def test_02(self):
          [...]

          def test_03(self):
          [...]


          While running test you can change,



          main.py



          from package import config

          config.DEFAULT_ENVIRONMENT = 'enviroment2'
          tc_scenario01 = unittest.TestLoader().loadTestsFromTestCase(TestScenario01)
          test_suite = unittest.TestSuite([tc_scenario01])
          HTMLReporterCustom.HTMLTestRunner().run(test_suite)


          You can also pass the environment name while running python main.py.



          main.py



          if __name__ == '__main__':
          config.DEFAULT_ENVIRONMENT = sys.argv[1] if len(sys.argv) > 2 else 'dev'
          tc_scenario01 = unittest.TestLoader().loadTestsFromTestCase(TestScenario01)
          test_suite = unittest.TestSuite([tc_scenario01])
          HTMLReporterCustom.HTMLTestRunner().run(test_suite)





          share|improve this answer














          You can maintain environment properties in separate config file.



          config.py



          DEFAULT_ENVIRONMENT='environment1'

          URL =
          'environment1': 'https://www.google.com/',
          'environment2': 'https://example.com/'



          Your Class,



          from package import config

          def MyClass(object):
          def __init__(self, driver):
          self.driver = driver

          def access_url(self):
          self.driver.get(config.URL[config.DEFAULT_ENVIRONMENT])


          Then test class will be as expected,



          class TestScenario01(unittest.TestCase):

          def setUp(self):
          [...]

          def test_01_access(self):
          MyClass(self.driver).access_url()

          def test_02(self):
          [...]

          def test_03(self):
          [...]


          While running test you can change,



          main.py



          from package import config

          config.DEFAULT_ENVIRONMENT = 'enviroment2'
          tc_scenario01 = unittest.TestLoader().loadTestsFromTestCase(TestScenario01)
          test_suite = unittest.TestSuite([tc_scenario01])
          HTMLReporterCustom.HTMLTestRunner().run(test_suite)


          You can also pass the environment name while running python main.py.



          main.py



          if __name__ == '__main__':
          config.DEFAULT_ENVIRONMENT = sys.argv[1] if len(sys.argv) > 2 else 'dev'
          tc_scenario01 = unittest.TestLoader().loadTestsFromTestCase(TestScenario01)
          test_suite = unittest.TestSuite([tc_scenario01])
          HTMLReporterCustom.HTMLTestRunner().run(test_suite)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 11 at 21:59

























          answered Nov 11 at 21:15









          Navarasu

          1,8671721




          1,8671721











          • This is so amazingly elegant! i was looking into all kinds of complicated solutions, it honestly never crossed my mind to put it in a variable and change it just before the .run() method. I am still wrapping my mind around all the programming concepts, so not only did you help me, but i also learned something. Thank you so much!
            – Red Alex
            Nov 11 at 21:54
















          • This is so amazingly elegant! i was looking into all kinds of complicated solutions, it honestly never crossed my mind to put it in a variable and change it just before the .run() method. I am still wrapping my mind around all the programming concepts, so not only did you help me, but i also learned something. Thank you so much!
            – Red Alex
            Nov 11 at 21:54















          This is so amazingly elegant! i was looking into all kinds of complicated solutions, it honestly never crossed my mind to put it in a variable and change it just before the .run() method. I am still wrapping my mind around all the programming concepts, so not only did you help me, but i also learned something. Thank you so much!
          – Red Alex
          Nov 11 at 21:54




          This is so amazingly elegant! i was looking into all kinds of complicated solutions, it honestly never crossed my mind to put it in a variable and change it just before the .run() method. I am still wrapping my mind around all the programming concepts, so not only did you help me, but i also learned something. Thank you so much!
          – Red Alex
          Nov 11 at 21:54

















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53253024%2fpython-unittest-how-to-chose-the-url-on-which-the-tests-are-executed%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







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3

          Museum of Modern and Contemporary Art of Trento and Rovereto