python - How to check a member variable with py.test skipif? -


in setup function in testcase set member variable is_type true or false. have few tests in class shouldn't run if value false. don't know how skipif functionality.

@py.test.mark.skipif( not ???.is_type ) def test_method(self):    ... 

self doesn't work ??? since it's not in object yet. how can refer is_type variable?


more complete example:

class dbtest(unittest.testcase):     def setup(self):         self.is_dynamo = os.environ.get( 'test_db', '' ) == 'dynamo'      def test_account_ts(self):         if not self.is_dynamo:             return          ... 

how can transform if not self.is_dynamo py.test skipif condition instead?

as mention, can't refer instance in skipif marker, since instance doesn't exist yet.

i keep simple , like:

@pytest.mark.skipif(os.environ.get('test_db') != 'dynamo') def test_dynamo():     db = mydatabasewithdynamo(is_dynamo=true)     # assertion or other. 

you can use @pytest.mark.skipif on classes too.

the documentation on feature has examples. many of them relate checking parts of environment decide tests should skipped. sounds similar use case, in company.

but if, in comment below, want avoid global variables, can raise pytest.skip exception wherever want. below i'm doing in fixture, think keeps test setup well-separated test cases. see the documentation more possibilities.

here test file:

import os import pytest  class mydatabasewithdynamo(object):     def __init__(self, is_dynamo):         pass  @pytest.fixture def database():     is_dynamo = os.environ.get('test_db') == 'dynamo'     if not is_dynamo:         raise pytest.skip()     return mydatabasewithdynamo(is_dynamo=true)  def test_db(database):     pass 

here test output:

$ py.test test_foo.py ======================================= test session starts ======================================= platform darwin -- python 2.7.5 -- py-1.4.20 -- pytest-2.5.2 collected 1 items  test_foo.py s  ==================================== 1 skipped in 0.01 seconds =================================== 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -