2

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

  • Why can't the test find __generate_potential_weeks
  • How can I test __generate_potential_weeks? It has some complex logic I would like to test separately from Scheduler

1 Answer 1

3

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)

Sign up to request clarification or add additional context in comments.

1 Comment

That solved it. I will accept the answer once SO lets me in a couple minutes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.