user12177026
user12177026

Reputation:

Python's unittest, pass test by input

I'm testing a code that plots a map, pretty much the only way to test it is to see with my own eyes the result, so I want to insert an input (Y/n) to the test function and if it is Y then the test will be considered as passed.

from unittest import TestCase
from .app import main
from .test_cases import test1


class Test(TestCase):
    def test_main(self):
        main(gdt1=test1[0],
             gdt2=test1[1],
             uav=test1[2])
        # This function plot the map, again, it doesn't matter what's the output for this question.
       worked = input('Enter y/n')
       if 'y' in worked: 
            # code to tell python the test passed.
        else:
            # code to tell python the test failed.

Upvotes: 1

Views: 231

Answers (2)

Greg
Greg

Reputation: 1991

For unittests, the biggest thing is the overall test exit code. If you want your test to only fail with an "n" input, simply fail the test:

from unittest import TestCase
from .app import main
from .test_cases import test1


class Test(TestCase):
    def test_main(self):
        main(gdt1=test1[0],
             gdt2=test1[1],
             uav=test1[2])
        # This function plot the map, again, it doesn't matter what's the output for this question.
       worked = input('Enter y/n')
       if worked == 'n':
           raise Exception('Test has failed!')

Upvotes: 0

stackErr
stackErr

Reputation: 4170

What you are looking for is AssertIn(). See: https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertIn

So your code would look like this:

class Test(TestCase):
    def test_main(self):
        main(gdt1=test1[0],
             gdt2=test1[1],
             uav=test1[2])
        # This function plot the map, again, it doesn't matter what's the output for this question.
       worked = input('Enter y/n')
       self.assertIn('y', worked)

You should probably use assertEqual() though since you are checking for equality so it would be self.assertEqual('y', worked.lower()). See: https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertEqual

Upvotes: 2

Related Questions