1

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?

1

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'
  • If the Apache server is on a remote machine, make sure you identify the OS on the remote machine, and not the OS where the test is running. – Dale Emery Jan 15 '17 at 0:46
1

That's a test in unittest, right? (guessing so by the method name)

If so, you could use assertTrue(), and check is the text in one of the expected values:

def test_web_interface(self):
    self.driver.get(DEST_URL)
    titles = ( 'Apache HTTP Server Test Page powered by CentOS',  'Apache2 Debian Default Page: It works')
    self.assertTrue(self.driver.title in titles)

And if my unittest guess was not correct, you could use the regular python assert:

assert self.driver.title in titles

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.