datacamp / pythonwhat

Verify Python code submissions and auto-generate meaningful feedback messages.
http://pythonwhat.readthedocs.io/
GNU Affero General Public License v3.0
67 stars 31 forks source link

SCT somehow creates error when correct code submitted #10

Closed hugobowne closed 8 years ago

hugobowne commented 8 years ago

In https://campus.datacamp.com/courses/1113/2464?ex=3 ,

when we submit the solution code, python throws an error. this did not happen when SCT wasn't present.

see screenshot:

screen shot 2016-05-27 at 11 57 10 am
vvnkr commented 8 years ago

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.

hugobowne commented 8 years ago

@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

hugobowne commented 8 years ago

@franciscastro about to try this now

franciscastro commented 8 years ago

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

hugobowne commented 8 years ago

@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

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
vvnkr commented 8 years ago

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.

vvnkr commented 8 years ago

Ok so I had a look at your problems:

hugobowne commented 8 years ago

@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?

vvnkr commented 8 years ago

They are fixed