Question

I have some Python tests that I run with Nose. An example is this:

class Logout(unittest.TestCase):

    def report_pass_fail(self):
        #code

    def setUp(self):
        #code

    def test_Logout(self):
        #code

    def tearDown(self):
        #code

Because I have a very big amount of tests and because i want to create a modular code architecture....i would like to have the report_pass_fail , setUp and tearDown methods inside a separate file(module) and just call them when needed inside the test's class.

I am not very experienced in OOP .I tried several combinations but with no success. How could I do that?

The module I created is this:

import json, httplib, base64, unittest, sys
from selenium import webdriver
import className
from creds import config, sauce_hub

class Fixtures(unittest.TestCase):
    def report_pass_fail(self):
        base64string = base64.encodestring('%s:%s' % (config['username'], config['access-key']))[:-1]
        result = json.dumps({'public': 'true', 'passed': sys.exc_info() == (None, None, None)})
        connection = httplib.HTTPConnection("saucelabs.com")
        connection.request('PUT', '/rest/v1/%s/jobs/%s' % (config['username'], self.wd.session_id), result, headers={"Authorization": "Basic %s" % base64string})
        result = connection.getresponse()
        return result.status == 200

    def setUp(self):
        desired_capabilities = webdriver.DesiredCapabilities.FIREFOX
        desired_capabilities['version'] = '4'
        desired_capabilities['platform'] = 'Linux'
        desired_capabilities['name'] = className.getName(self)
        desired_capabilities['record-video'] = False

        self.wd = webdriver.Remote(desired_capabilities=desired_capabilities,
                                   command_executor="http://" + config['username'] + ":" + config['access-key'] + sauce_hub)
        self.wd.implicitly_wait(10)

    def tearDown(self):
        self.wd.quit()

And its use in the test file is something like this:

import unittest
from fixture_module import Fixtures
#is_alert_present(wd)

#credentials



class DeleteLectureFromCoursePanel(Fixtures,unittest.TestCase):

    import fixture_module

    f = fixture_module.Fixtures()
    f.report_pass_fail()

    f.setUp()

    def test_DeleteLectureFromCoursePanel(self):
        success = True
        wd = self.wd
        wd.find_element_by_link_text("Delete").click()
        self.assertEqual("Are you sure?", wd.switch_to_alert().text)
        wd.switch_to_alert().accept()
        self.assertTrue(success)

    f.tearDown()


if __name__ == '__main__':
    unittest.main()

It seems ok and there are no errors in PyCharmIDE but when I run it from console it says :

ValueError:no such test method in <class 'fixture_module.Fixtures'> : runTest
Was it helpful?

Solution

You are making this much much more complicated than it needs to be. If your DeleteLectureFromCoursePanel inherits from Fixtures, it already gets the methods defined in that class. They will be called automatically by the test runner, and there's no reason for you to call them manually (especially within the class body like that).

There's also no need for you to additionally inherit from TestCase. You get that already via Fixtures.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top