alexdriedger
alexdriedger

Reputation: 3034

Python - Testing class methods with unittest

I have a Scheduler class and a test written for __generate_potential_weeks using unittest

main.py

class Scheduler:
    def __init__(self, num_teams, num_weeks):
        self.potential_weeks = self.__generate_potential_weeks(num_teams)
        # Other stuff

    def __generate_potential_weeks(self, num_teams):
        # Does some things

test_scheduler.py

import unittest
from main import Scheduler

class SchedulerTestCase(unittest.TestCase):
    def test_generate_weeks(self):
        sch = Scheduler(4, 14)
        weeks = sch.__generate_potential_weeks(4)

When I try to test __generate_potential_weeks, I get the following error

AttributeError: 'Scheduler' object has no attribute '_SchedulerTestCase__generate_potential_weeks'

Questions

Upvotes: 2

Views: 1182

Answers (1)

wholevinski
wholevinski

Reputation: 3828

Double underscore has a special meaning within python. The name will be mangled to avoid a conflicting name in a subclass.

If that wasn't your intent, I'd encourage you to mark it with a single underscore. If it was, you can still access the function by using the mangled name. I think this will work for you: sch._Scheduler__generate_potential_weeks(4)

Upvotes: 3

Related Questions