A logical "or" inside a Selenium test case



  • Assuming I would like test for a default Apache server response at one point, and I would like to make it platform agnostic.

    For CentOS I have:

      def test_web_interface(self):
        self.driver.get(DEST_URL)
        self.assertIn('Apache HTTP Server Test Page powered by CentOS', self.driver.title)
    

    for Debian I have:

      def test_web_interface(self):
        self.driver.get(DEST_URL)
        self.assertIn('Apache2 Debian Default Page: It works', self.driver.title)
    

    The assertion string is the only difference in two tests.

    If I would like to avoid passing the "tested platform"-argument to the test, apart from checking the common string like Apache or Page, what would be a canonical way of including a disjunction inside such a unit test?



  • This may not be the kind of solution that you have in mind, and is likely not cannonical, but I have solved a similar issue by using environment variables. For example you could do:

    import os
    
    def test_web_interface(self):
        self.driver.get(DEST_URL)
        platform = os.environ['TEST_PLATFORM']
        titles = {
            'centos': 'Apache HTTP Server Test Page powered by CentOS',
            'debian': 'Apache2 Debian Default Page: It works'
        }
        self.assertIn(titles.get('platform'), self.driver.title)
    

    This way you can add more platforms if necessary, and all you need to do is set the environment variable in a config or elsewhere on each platform:

    import os
    os.environ['TEST_PLATFORM'] = 'whatever_platform'
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2