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 fromScheduler
rpanai
13.5k3 gold badges48 silver badges65 bronze badges
asked Feb 21, 2019 at 13:19
alexdriedger
3,0542 gold badges27 silver badges30 bronze badges
1 Answer 1
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)
answered Feb 21, 2019 at 13:25
wholevinski
3,84821 silver badges25 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
alexdriedger
That solved it. I will accept the answer once SO lets me in a couple minutes
lang-py