Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
unittest
#1
So I'm working with Python and wanted to understand something specific to the unittest module. I'll include all snippets. please note that everything runs. My question is in the comment in the last snippet.

SNIPPET 1 OK
class SurveyTaker():
	def __init__(self, query):
		self.query = query
		self.responses = []
	def show_query(self):
		print(self.query)
	def update_resp(self, new_item):
		self.responses.append(new_item)
		print(new_item, '- uploaded.')
	def show_resp(self):
		for i in self.responses:
			print('- ', i)
SNIPPET 2 OK
from asurvey2 import SurveyTaker

title = 'Something grand'.upper()
my_survey=SurveyTaker(title)

while True:
	
	print('\'q\' to quit anytime')
	comment = str(input('say something. '))
	
	if comment == 'q':
		break
	else:
		comment = my_survey.update_resp(comment)
		print('thx')
my_survey.show_resp()
SNIPPET 3 OK -- QUESTION I have is in this snippet regarding self.assertIn(foo, self.bar)....
#test_asurvey2.py

import unittest
from asurvey2 import SurveyTaker

class TestIsh(unittest.TestCase):
	def testSurvey(self):
		titl = 'something said'.title()
		my_survey = SurveyTaker(titl)
		responses = []
	
		for item in responses:
			my_survey.update_resp(item)
'''

'''	
		for item in responses:
            #I would think line below should read -- self.assertIn(item, self.my_survey.responses)
            #or self.assertIn(item, self.my_survey.show_resp) ...
			self.assertIn(item, self.my_survey)#why does self.my_survey actually work?  IDU this!
unittest.main()
Reply
#2
My guess is that assertIn(a, b) method which checks if a is in b is clever enough to be recursive, meaning that if b is a complex object, the method iterates through each property of the object.
Reply
#3
You are not not updating responses in SNIPPET 3.
>>> titl = 'something said'.title()
>>> my_survey = SurveyTaker(titl)
>>> responses =
>>> for item in responses:
...    my_survey.update_resp(item) 
...    
>>> responses
[]
So when you do for item in responses:
item will be empty and you check if item is in in self.my_survey Confused
How do you get this to work i don't know.

assertIn() is for test container membership.
So for a list can eg test if 4 is in [1, 2, 3].
# uni_in.py
import unittest

class ContainerMembershipTest(unittest.TestCase):

    def test_list(self):
        self.assertIn(4, [1, 2, 3])
This will fail with AssertionError.
Output:
λ python -m unittest uni_in.py F ====================================================================== FAIL: test_list (uni_in.ContainerMembershipTest) ---------------------------------------------------------------------- Traceback (most recent call last):   File "C:\Python36\uni_in.py", line 7, in test_list     self.assertIn(4, [1, 2, 3]) AssertionError: 4 not found in [1, 2, 3] ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (failures=1)
Fix self.assertIn(3, [1, 2, 3])
Output:
λ python -m unittest uni_in.py . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK
Reply
#4
Quote:So when you do for item in responses:
item will be empty and you check if item is in in self.my_survey Confused
How do you get this to work i don't know.

sorry about that...
class Testing(unittest.TestCase):
   def testThis(self):
        responses = [1,2,3]#or ['a','b','c'] is fine
So are the assert methods totally recursive? What am I missing here?

b/c
#1
for item in responses:
   self.assertIn(item,self.my_survey.responses)
I would this this code would be the **only** right one.
#2
for item in responses:
   self.assertIn(item, self.my_survey.show_resp)
and this snippet also works:
#3
for item in responses:
   self.assertIn(item, self.my_survey.foo)
IDU python's internal 'mechanism'/'logic' in this example. I'd think snippet 2 only should work...but they all produce the same output.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Unittest roadrage 0 1,616 Feb-14-2019, 06:11 AM
Last Post: roadrage

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020