Results 1 to 7 of 7

Thread: Test scripts in python

  1. #1
    Join Date
    Sep 2009
    Beans
    158
    Distro
    Ubuntu 12.04 Precise Pangolin

    Test scripts in python

    I was trying to make a testing script in python, but I don't understand it completely.
    I understand that I need this (not sure if this is exactly it, but it's at least pretty close):

    python program.py < test1.in | diff - test1.out

    basically, here is my folder structure.
    program.py
    tests/
    test1.in
    test2.in
    test1.out
    test2.out
    other.in
    other.out

    I want a .sh file which will repeatedly run that line for all test cases. Basically, here's my pseudo code
    get tests/*.in
    for each result:
    run "python program.py < tests/testname.in | diff - testname.out"
    The box said 'Windows 7 or better' .... so I installed Linux.
    I5 2500K @ 4.6 GHZ, 5770 stockOC, 902 case, OCZvertexII, 2 1TB seagate RAID0, 16GB @ 1600 MHz

  2. #2
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: Test scripts in python

    Code:
    for i in *.in; do file=`basename -s .in "$i"`; python program.py < $file.in | diff - $file.out; done
    「明後日の夕方には帰ってるからね。」


  3. #3
    hakermania's Avatar
    hakermania is offline Τώρα ξέρεις τι γράφω εδώ!
    Join Date
    Aug 2009
    Location
    Greece
    Beans
    1,705
    Distro
    Ubuntu Development Release

    Thumbs down Re: Test scripts in python

    Quote Originally Posted by Bachstelze View Post
    Code:
    for i in *.in; do file=`basename -s .in "$i"`; python program.py < $file.in | diff - $file.out; done
    Nice solution

    I cannot help but comment that you should prefer $() instead of ``

  4. #4
    Join Date
    Sep 2009
    Beans
    158
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Test scripts in python

    when I run it I get this message:
    Code:
    basename: invalid option -- 's'
    Try `basename --help' for more information.
    ms.sh: 1: cannot open .in: No such file
    diff: .out: No such file or directory
    basename: invalid option -- 's'
    Try `basename --help' for more information.
    diff: .out: No such file or directory
    ms.sh: 1: cannot open .in: No such file
    basename: invalid option -- 's'
    Try `basename --help' for more information.
    diff: .out: No such file or directory
    ms.sh: 1: cannot open .in: No such file
    I'm running ubuntu 11.04, if it helps.
    Also, how do I make it so instead of looking for .in files, it looks for .in files in the tests directory.
    The box said 'Windows 7 or better' .... so I installed Linux.
    I5 2500K @ 4.6 GHZ, 5770 stockOC, 902 case, OCZvertexII, 2 1TB seagate RAID0, 16GB @ 1600 MHz

  5. #5
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: Test scripts in python

    Hmm yeah, my mistake, the -s syntax is non standard. Instead do

    Code:
    for i in *.in; do file=`basename "$i" ".in"`; python program.py < $file.in | diff - $file.out; done
    Quote Originally Posted by gameguy View Post
    Also, how do I make it so instead of looking for .in files, it looks for .in files in the tests directory.
    Just add a cd test at the top of your script.
    「明後日の夕方には帰ってるからね。」


  6. #6
    Join Date
    Sep 2009
    Beans
    158
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Test scripts in python

    Code:
    cd tests
    echo "Entered directory tests"
    for i in *.in;
        do file=`basename "$i" ".in"`;
        echo "$file found"
        python ../main.py < $file.in | diff - $file.out;
        done
    Thanks a heap. A few minor modifications needed to be done, and I decided to make it span over several lines, but that was easy enough.
    Thanks
    The box said 'Windows 7 or better' .... so I installed Linux.
    I5 2500K @ 4.6 GHZ, 5770 stockOC, 902 case, OCZvertexII, 2 1TB seagate RAID0, 16GB @ 1600 MHz

  7. #7
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: Test scripts in python

    A more conventional way to indent it would be

    Code:
    cd tests
    echo "Entered directory tests"
    for i in *.in; do
        file=`basename "$i" ".in"`;
        echo "$file found"
        python ../main.py < $file.in | diff - $file.out;
    done
    And you don't need the semicolons (except the one in the for line if you put do on the same line, otherwise you can omit that one too).
    「明後日の夕方には帰ってるからね。」


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •