-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
does pytest support to this way to collect test cases? #1123
Comments
Instead if making a wrapper functioning, just add a Also you can override collection from the pytest side A native But can also easily be implemented in a conftest |
the most simple method should be like (i didn't run it on my machine and it needs a recent py.test) def test(func):
func.__test__ = True
return func
class TestCase(object):
@test
def true(self): # this method name will be test_true
assert 0 == 0
@test
def false(self): # this method name will be test_false
assert 1 == 0 also closing the issue for now |
@RonnyPfannschmidt Thank you for your comment. But that seems to be not the style I want. def _test(comment): # can not use `test`, because it will be collected if do not change the default
def middle(func):
def wrapper(*args, **kwargs):
func.__test__ = True
func.__name__ = '_'.join(['test'] + comment.split(' ')) # build test method name with comment
func.__doc__ = comment
return func(*args, **kwargs)
return wrapper
return middle
class TestCase(object):
@ _test('true condition when 0')
def _(self): # this method name will be `test_true_condition_when_0` dynamically
assert 0 == 0
@ _test('false condition when 1')
def _(self): # this method name will be `test_false_confition_when_1` dynamically
assert 1 == 0 python does not support method overload, and I tried, but the result will be just only 1 test method collected, how to custom pytest to collect like this way? |
you can use call stack inspection, and put the tests into the locals of the calling namespace or you create a custom object in the locals to begin with class Testcase(object):
_test = TestStore()
@_test('hi there')
def _(self):
pass you will need to extend the reporting as well if you want to use this style |
is there anywhere documented how to migrate "nose2 type of tests that yield self.assert* and each yield is collected as a separate test" into pytest? |
@Oviwankenobi that kind of test got deprecated in pytest as its not compatible with setupstate and fixtures currently its sugested not to do them, |
Automatically no, our suggestion is to convert them to parametrized tests: https://docs.pytest.org/en/latest/parametrize.html?highlight=parametrize. It is usually straightforward, but if you get into trouble you can post the test code and we can help converting it. |
@nicoddemus i presume @Oviwankenobi is facing the same issue moinmoin faced - pytest completely messed up context-bearing yield tests - and it wont ever redeem them |
@nicoddemus @RonnyPfannschmidt thank you for your input I am not sure how parameterize feature can help but maybe I am not getting it right so here's the tests pattern I am looking at
list_of_files is dynamic, no hardcoded option here. |
@Oviwankenobi is the list of files obtained at collection time, IOW roughly during importing of the test module? If it is: @pytest.mark.parametrize('input_test_file', list_of_files)
def test_case_1(input_test_file):
assert _check_file(input_test_file) However if |
@Oviwankenobi take a look at sybil perhpas - its about generating test items from parsed files and supports supplying own parsers * has a basic pytest integration |
thanks guys, I end up developing a custom solution: a base class for all our tests which implements an asserts method which knows to collect all errors instead of stopping the test and at the end add to the final test report the collected errors. |
Hmm hard to tell without looking at the code... does this base class subclass |
in pytest you do not have to derive your test from a base class of unittest.TestCase the errors I collect are those of type "assert errors (e.g. val1 == val2)", I collect them in an dictionary and through the below hook I add them into the final report
|
I am not sure if I am more clear in this post ... :) thanks a lot for your time |
I want to shorten test method name, and just rewrite method name with comment, like this:
Yeah, it does not work, but how to customise pytest to implement this style?
The text was updated successfully, but these errors were encountered: