简体   繁体   中英

Python unit test cases

I am new to python and i have written some code but finding difficulty for writing test cases for the same problem. The problem statement comes from here . Here is my solution code:

def get_input_stacks():
  c=list(map(int,input().split()))
  sc=list(map(int,input().split()))
  return c,sc

def Main():
  p,s=map(int,input().split())
  print(" I am calling through main")
  i=0
  differ=list()
  c=list()
  sc=list()
  for i in range(s):
    differ.append([])
  for i in range(p):
    n=0

    c,sc=get_input_stacks()
    dictionary=dict(zip(c,sc))
    c.sort()

    for j in range(s-1):
      if dictionary[c[j]]>dictionary[c[j+1]]:
        n+=1
    differ[n].append(i+1)
  for i in range(s):
    for j in range(len(differ[i])):
      print(differ[i][j])

if __name__ == '__main__':
  Main()

And for testing purposes i have written the following code:

from unittest.mock import patch
import unittest

import confusion

class ContainersTestCase(unittest.TestCase):
  def test_get_input_stacks_processed_input_correctly(self):
    user_input = [ '3 3' '16 24 60' '498 861 589'
                   '14 24 62' '72 557 819' '16 15 69' '435 779 232' ]
    expected_stacks = [ '2' '1' '3' ]

    with patch('builtins.input', side_effect=user_input):
        stacks =confusion.get_input_stacks()
    self.assertEqual(stacks, expected_stacks)

if __name__ == '__main__':
unittest.main()

But I am getting following error:

ERROR: test_get_input_stacks_processed_input_correctly (__main__.ContainersTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "testing.py", line 26, in test_get_input_stacks_processed_input_correctly
    stacks =confusion.get_input_stacks()
  File "G:\python learn\confusion.py", line 5, in get_input_stacks
    sc=list(map(int,input().split()))
  File "C:\Users\Vanshu Hassija\AppData\Local\Programs\Python\Python37-32\lib\unittest\mock.py", line 951, in __call__
    return _mock_self._mock_call(*args, **kwargs)
  File "C:\Users\Vanshu Hassija\AppData\Local\Programs\Python\Python37-32\lib\unittest\mock.py", line 1010, in _mock_call
    result = next(effect)
StopIteration

I am not sure if these errors are just in you post or also in your code but just try and check the following parts:

  1. The lists in your test code don't have a "," as separator.
  2. Your source code and test code has so bad indention on various spot's.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM