title: Using url_for in tests
body: <p>I am writing some tests for a Flask application and am running into an issue where the URLs in my test_client responses do not match those produced by calling url_for from within the test case.  For example, I have the following in my template:</p>
  
  <pre><code>&lt;li&gt;&lt;a href="#" onClick="requestReport('{{ url_for('report') }}', '{{ session['email'] }}');"&gt;Report&lt;/a&gt;&lt;/li&gt;
  </code></pre>
  
  <p>The response from the test_client renders this as:</p>
  
  <pre><code>&lt;li&gt;&lt;a href="#" onClick="requestReport('/api/report', 'user@domain.com');"&gt;Report&lt;/a&gt;&lt;/li&gt;
  </code></pre>
  
  <p>I have a test case that checks to make sure this URL appears on a page under certain conditions:</p>
  
  <pre><code>self.client = self.app.test_client(use_cookies=True)
  with self.app.app_context():
      page = self.client.get(url_for("index"), follow_redirects=True).data.decode()
      assert url_for("report") in page
  </code></pre>
  
  <p>The problem is that this test will fail even when the URL appears on the page, because the url_for call in the template is producing different output than the url_for call in my test case.  If I print url_for("report") from the test case code, I get:</p>
  
  <pre><code>http://localhost:5000/api/report
  </code></pre>
  
  <p>I have the SERVER_NAME key in app.config set to "localhost:5000" because if I do not have SERVER_NAME set the test case code will throw this error:</p>
  
  <pre><code>RuntimeError: Application was not able to create a URL adapter for request independent URL generation. You might be able to fix this by setting the SERVER_NAME config variable.
  </code></pre>
  
  <p>Obviously, I could work around this by hard-coding the URL in my test case code, but I would prefer to use URL_for so that future changes to my URL do not break my test code.</p>
  
  <p>I have tried a couple different strings as SERVER_NAME, including "" which just generates a malformed URL that still doesn't match the one generated in the response.</p>
  
  <p>Is there some way to fix this other than hard-coding the URL?</p>