Skip to content

How to pytest raw_input calls #3504

Closed
Closed
@ridhwaans

Description

@ridhwaans

Pytest newbie here
Want to make three test cases that take in None, nonexistant filenames and whitespace filenames for raw_input in main(prompt), and assert the print output
Code in question

#!/usr/bin/env python
import os
import unittest.mock as mock
import pytest

'''Print file
'''
def main(prompt):
  filename = raw_input(prompt) #ask for filename using the given prompt
  if validate(filename):
    for line in open(filename.strip(), "r").readlines(): 
        print line
  else:
    print "The file you selected does not exist, please try again"
    main(prompt) #repeat this function if the use did not give valid input

def validate(input):
    input = input.strip()
    if input is None:
        return False
    elif input == '':
        return False
    elif not (os.path.exists(input)):
        return False
    else:
        return True

if __name__ == "__main__":
    main("Select file in current directory:")

Using: python 2.7, pytest latest version

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: questiongeneral question, might be closed after 2 weeks of inactivity

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions