Git Product home page Git Product logo

Comments (9)

vvnkr avatar vvnkr commented on June 23, 2024

So these are issues of scoping. The problem is that test_function() and test_object_after_expression() will run parts of the code in the global scope, but word and shout_word are defined only in the function's scope.

In test_object_after_expression() you can use the argument context_vals to work around this problem. To context_vals you can assign a list of values, which will be assigned to the variable in the context its surrounding test. If it's used within test_function_definition(), the context variables will be the arguments of the function. For example,

*** = solution
def shout(word):
    shout_word = word + '!!!'
*** =sct
test_function_definition('shout', arg_names = False, body = lambda: test_object_after_expression("shout_word", context_vals = ["shouting this"]))

This test will test the object shout_word after running through the body of shout() with its only argument, word, set to "shouting this". In other words, using context_vals you can mock the values of the arguments while testing. Note that this test implies nothing about how you call the argument, you could call it foo instead of word and the test would still past.

You can also use test_object_after_expression() within a test_for_loop() for example, in this case the context_vals will be the values of the loop variable.

If you use test_function() will currently just run in the global scope, and there's no way to set the arguments when running the tests.

I'm thinking of ways to make this less complex and more intuitive.

I think it might be easier to use the outputs argument of test_function_definition() to test whether correct values are printed by the function.

from pythonwhat.

hugobowne avatar hugobowne commented on June 23, 2024

@vincentvankrunkelsven thank you! this workaround doesn't look as complex as some that were used during the dev of testwhat ;-)

@franciscastro try this out (should work for both problems that we had) and we'll let @vincentvankrunkelsven know how it goes asap

from pythonwhat.

hugobowne avatar hugobowne commented on June 23, 2024

@franciscastro about to try this now

from pythonwhat.

franciscastro avatar franciscastro commented on June 23, 2024

It doesn't seem to work for cases when I have to test an object within a function definition, but the function does not have parameters.

05-27-2016-test-function-in-def-error-5-1

from pythonwhat.

hugobowne avatar hugobowne commented on June 23, 2024

@vincentvankrunkelsven the context_vals workaround doesn't seem to be working for us.

In the case that the function we're defining takes no arguments we have tried

  • context_vals = [] (then we get above error 'list index out of range')
    • we have also tried along context_vals as a list of one element and in this case we get the 'not defined' error.

this occurs in https://campus.datacamp.com/courses/1113/2464?ex=3

Also, in https://campus.datacamp.com/courses/1113/2464?ex=4 , the function in which takes an argument, using context_vals=["congratulations!!!"] throws the not defined error. see screenshot.

any other workarounds?

It looks like @franciscastro 's SCTs are written correctly but maybe you want to take a quick look also

screen shot 2016-05-27 at 4 58 41 pm

from pythonwhat.

vvnkr avatar vvnkr commented on June 23, 2024

Seems like it doesn't work as I expected, I'll have a look and it'll be fixed in the next patch. That will probably be put live Monday evening my time, so that would be Monday around noon for you.

from pythonwhat.

vvnkr avatar vvnkr commented on June 23, 2024

Ok so I had a look at your problems:

  • For exercise 3: setting context_vals = [] caused an error. However for the effect you desire, just don't define context_vals, it will be set to None by default, and this will provide the correct behaviour. I added a check in a new version, that causes [] to be treated like None for the context_vals argument.
  • For exercise 4: the problem here is not caused by test_object_after_expression(), but rather by test_function() in your fourth test_function_definition(). As I mentioned before, test_function() is by design only capable of testing arguments in the global scope. (It doesn't have something like context_vals, I think it maybe should). You can check whether the function print() is used by using test_function('print', args=[]) and you can check if the correct output is given in the function definition by using: outputs of the test_function_definition() function. I'm working on a better way of handling the scopes, so test_function() can be used properly within other test functions.

from pythonwhat.

hugobowne avatar hugobowne commented on June 23, 2024

@vincentvankrunkelsven neither of these seem to be working still. perhaps you and I should speak for 5min and/or you could write an example SCT that you think should work?

from pythonwhat.

vvnkr avatar vvnkr commented on June 23, 2024

They are fixed

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.