getslash / slash

The Slash testing infrastructure
https://getslash.github.io/slash
Other
75 stars 38 forks source link

All test parametrize functions are called #1046

Open esterfarbstein opened 3 years ago

esterfarbstein commented 3 years ago

Hi, I have some tests in the same file. For each test the parametrize decorator calls a function. for example:

def get_tests():
    return [1 2 3]

@slash.parametrize('test_data', get_tests())
@slash.tag('a')
def test_a(test_data):
    pass

@slash.parametrize('test_data', get_tests())
@slash.tag('b')
def test_b(test_data):
    pass

When I run just one test, for example -k "a" : the get_tests() function have been called twice or even more - when I add more tests with the same behavior. How can I change this behavior that the get_test will be called just one time from the specific test that I want to run?

Thanks, Ester

vmalloc commented 3 years ago

The get_tests function is not called from the test itself per se. It is called by the loader to attach the parametrization to tests, and in your example it should be called only once per test (twice in this particular snippet). Is this not what happens?

esterfarbstein commented 3 years ago

@vmalloc , get_testsis called once per test - that's included tests that I don't want them to run now. If I will have 10 tests in this file - so the get_tests function will be called 10 times - although I want it to be called just one time for the specific test that I called. (Also, if I use global variables in the get_tests function they will be override and will get their value from the last test in this file)

So, can I run this function just for the function that I called?

Thanks, Ester

vmalloc commented 3 years ago

Well, this is kind of a chicken-and-egg problem, isn't it? The exact value of the parametrization is dependent on get_tests, and this means that whether or not -k a will apply to the test or not is dependent on the evaluation of get_tests()... So I don't see a way around it without modifying the code... What exactly do you want get_tests to do here?

esterfarbstein commented 3 years ago

I don't really understand why we need to call parametrization of test_b if we just run test_a (-k "a")? I expect that just the test_a parametrization will happen Is the parametrization happens before we are looking for the specific tests with the requested tag?

vmalloc commented 3 years ago

-k is substring search, so if you have a test that is parametrized with x=bla, and you pass -k bla, the test will be run. If not, it won't.

esterfarbstein commented 3 years ago

@vmalloc thank you for your quick and full answers. I tried to use now the tag search option -k tag:a and also now it calls the parameterize of test_b function although this function's tag not contains "a". Why do we need to call test_b parameterize?

vmalloc commented 3 years ago

Actually it's not Slash that calls it - it is you:

@slash.parametrize('test_data', get_tests())
@slash.tag('a')
def test_a(test_data):
    pass

is equivalent to writing

_temp_var = get_tests()
@slash.parametrize('test_data', _temp_var))
@slash.tag('a')
def test_a(test_data):
    pass

As you can see it is called by the module being loaded.... There is no actual way in Python to load your test file without going through all of these calls...