Git Product home page Git Product logo

Comments (1)

vvnkr avatar vvnkr commented on September 24, 2024

@hugobowne I found the problem here, when you use test_function('print', 1), pythonwhat will find the first print() function, and see if its evaluated arguments are the same in the student and solution scope. In other words, pythonwhat is executing file.read() in both environments. However, it uses the file object at the end of the execution, which is a closed file. Hence the error message, thrown within the pythonwhat package.

What's important to remember here is that all evaluations in the pythonwhat package (for example evaluating the arguments of a function to compare them) are done in the ending scope, this means the scope of variables that we end up with after executing all the student/solution code. Doing tests on arguments/objects that are changed through the code (like the file object is changed by the file.close() method) can cause errors/strange behaviour. Same problem exists in testwhat package.

It's not easy to improve this and still have a performant testing package (it can be done, but it costs time and effort), hence I'll provide you with some workarounds:

  • Use test_function("print", 1, do_eval = False), which will test the argument of print() on its text (== file.read()) instead of its value. Note that things like print( file.read() ) will still work though.
  • Use test_expression_output() which will run the complete script in student and solution scope and assert that their value is the same.
  • Use test_output_contains(/.../) fill in the dots with a part that you know should be in the text they have to print out.

Hope this helps

from pythonwhat.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.