Python Forum
unittest - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: unittest (/thread-7097.html)



unittest - mepyyeti - Dec-21-2017

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()



RE: unittest - squenson - Dec-21-2017

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.


RE: unittest - snippsat - Dec-21-2017

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



RE: unittest - mepyyeti - Dec-21-2017

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.