How to test a Python script with an input file filled with testcases?

前端 未结 4 945
情歌与酒
情歌与酒 2021-01-24 18:00

I\'m participating in online judge contests and I want to test my code with a .in file full of testcases to time my algorithm. How can I get my script to take input from this .i

4条回答
  •  無奈伤痛
    2021-01-24 18:41

    PyUnit "the standard unit testing framework for Python" might be what you are looking for.


    Doing a small script that does something like this:

    #!/usr/bin/env python
    import sys
    
    def main():
        in_file = open('path_to_file')
        for line in in_file:
            sys.stdout.write(line)
    
    if __name__ == "__main__":
        main()
    

    And run as

    this_script.py | your_app.py
    

提交回复
热议问题