Comments (9)
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.
@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.
@franciscastro about to try this now
from pythonwhat.
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.
from pythonwhat.
@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.
- we have also tried along
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
from pythonwhat.
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.
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 definecontext_vals
, it will be set toNone
by default, and this will provide the correct behaviour. I added a check in a new version, that causes[]
to be treated likeNone
for thecontext_vals
argument. - For exercise 4: the problem here is not caused by
test_object_after_expression()
, but rather bytest_function()
in your fourthtest_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 likecontext_vals
, I think it maybe should). You can check whether the functionprint()
is used by usingtest_function('print', args=[])
and you can check if the correct output is given in the function definition by using:outputs
of thetest_function_definition()
function. I'm working on a better way of handling the scopes, sotest_function()
can be used properly within other test functions.
from pythonwhat.
@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.
They are fixed
from pythonwhat.
Related Issues (20)
- Better support to check pandas chains
- Get rid of test_with()
- More clearly document check_args() for check_function_def()
- Fix old SCTs that fail assertions HOT 2
- check_class_def support HOT 6
- check_function inside for loop can't find aliases outside
- Expose and document `has_error()` function
- Incorrect highlighting because of flaws in ObjectAssignmentParser
- Add article about message generation
- Improve API for checking the signature in check_function_def
- Reference: check_object missing explanation of two params HOT 1
- Unable to Install HOT 2
- Document the order to check chained functions
- The easy way to extract the message associated with "TestFail" HOT 2
- Specifying filenames to setup_state() HOT 2
- Add a tolerance argument to has_equal_value()
- Add a test based on the line of a code
- check_function() not working as documented (or what am I missing?)
- has_output() doesn't work with two 'print'
- Small documentation error on check_function_def
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pythonwhat.