title: How to skip tests based on condition in pytest?
body: <p>Below is the snippet of <code>conftest.py</code>, which accepts command line arguments.</p>
  <p><strong>conftest.py</strong></p>
  <pre><code>import sys,json,pytest
  import logging
  
  def tear_down():
      logging.info(&quot; tear-down after all tests&quot;)
  
  def pytest_addoption(parser):
  
      parser.addoption(&quot;--a1&quot;, action=&quot;store&quot;, default=&quot;default_a1&quot;,
                       help=&quot;value of a1&quot;)
  
      parser.addoption(&quot;--b1&quot;, action=&quot;store&quot;, default=&quot;default_b1&quot;,
                       help=&quot;value of b1&quot;)
  
  
   @pytest.fixture(scope=&quot;class&quot;,autouse=True)
  def set_up(request):
      logging.info(&quot;set-up before all tests&quot;)
  
      a1 = request.config.getoption(&quot;--a1&quot;)
      b1 = request.config.getoption(&quot;--b1&quot;)
  
      if request.cls.__name__ == 'Test1':
          return([a1,b1])
  
      request.addfinalizer(tear_down)
  </code></pre>
  <blockquote>
  <p>Below snippet is of <code>test.py</code></p>
  <p>Whenever the <code>counter value not equal to 1</code> i.e., <code>c1_1 != 1</code>
  <code>(test_2)</code> then it should skip all the following cases. Probably I can
  try checking this condition in each and every test-case and then
  <code>pytest.skip or assert False</code>. But this is not great idea.</p>
  <p>I tried marker <code>pytest.mark.skipif</code> , but within that marker <code>&quot;can't access variable skip_all&quot; (either &quot;Test1.skip_all&quot;)</code> <code>I would like to skip tests based on certain condition.</code> If my below approach is not right, please help me
  with right approach to skip tests based on condition.</p>
  </blockquote>
  <p><strong>test.py</strong></p>
  <pre><code>import os,sys
  import pytest
  from other import other_class
  
  a1_1 = b1_1 = c1_1 = &quot;&quot;
  
  class Test1:
     skip_all = False
  
      def test_1(self,set_up):
          try:
              global a1_1,b1_1
              a1_1 = set_up[0]
              b1_1 = set_up[1]
          except Exception as err:
              logging.error(err)
              Test1.skip_all = True
              assert False, err
              
      @pytest.mark.skipif(skip_all == True, reason=&quot;Invalid&quot;)
      def test_2(self):
          global c1_1
          c1_1 = obj.func1(a1_1,b1_1)
          if c1_1 != 1:      #rest all test-cases shouldn't execute
             logging.error(&quot;Invalid&quot;)
             Test1.skip_all = True
             
          d1_1 = obj.func2(a1_1, c1_1)
          if d1_1 == 0:
             assert False, &quot;zero&quot;
          
      @pytest.mark.skipif(skip_all == True, reason=&quot;Zero&quot;)
      def test_3(self):
          d1_1 = obj.func2(b1_1, c1_1)
          if d1_1 == 0:
             assert False, &quot;zero&quot;
  
      @pytest.mark.skipif(skip_all == True, reason=&quot;Zero&quot;)
      def test_4(self):
         d1_1 = obj.func2(a1_1, c1_1)
         if d1_1 == 0:
            assert False, &quot;zero&quot;
  
  obj = other_class()
  </code></pre>
  <p>Am I missing anything ? or anything to do with fixtures ?
  Is this possible ? Can anyone please let me know how to do that ?</p>
  <p>In below example, how can we use external variable that's accessible in <code>marker:skipif</code> , instead of <code>'sys.platform'</code></p>
  <pre><code>import sys
  import pytest
  
  @pytest.mark.skipif(sys.platform != 'darwin', reason=&quot;Mac tests&quot;)
  def test_mac():
      assert True
  
  @pytest.mark.skipif(sys.platform != 'linux', reason=&quot;Linux tests&quot;)
  def test_linux():
      assert True 
  
  @pytest.mark.skipif(sys.platform != 'win32', reason=&quot;Windows tests&quot;)
  def test_windows():
      assert True 
  
  @pytest.mark.skip(reason=&quot;To show we can skip tests without any condition.&quot;)
  def test_any():
      assert True
  </code></pre>