title: How to unit test function that requires an active Click context in Python
body: <p>I am working on a command-line shell, and I'm trying to test some functions that parse command arguments.</p>
  <h3>parser.py:</h3>
  <pre class="lang-py prettyprint-override"><code>import shlex
  import click
  
  
  def process_cmd(args, called_self=False):
      result = args
  
      # Replace aliases
      if not called_self:
          aliases = click.get_current_context().obj.config['ALIASES']
          if result[0] in aliases:
              substitution = process_cmd(split_args(aliases[result[0]]), True)
              if len(result) == 1:
                  result = substitution
              else:
                  result = substitution + result[1:]
  
      return result
  
  
  def split_pipeline(args):
      cmd = []
      for arg in args:
          if arg == '|':
              yield process_cmd(cmd)
              cmd = []
          else:
              cmd.append(arg)
      # yield the last part of the pipeline
      yield process_cmd(cmd)
  </code></pre>
  <p>This is a unit test I wrote for <code>split_pipeline</code>:</p>
  <pre><code>import parser
  
  def test_pipeline():
      args = ['1', '2', '|', '3', '|', '4']
      result = [i for i in parser.split_pipeline(args)]
      assert result == [['1', '2'], ['3'], ['4']]
  </code></pre>
  <p>When this unit test is run, I get an error saying that there is no active Click context.</p>