"""Tests for the Wallace API."""

import subprocess
import re
import requests


class TestHeroku(object):

    """The Heroku test class."""

    def test_sandbox(self):
        """Launch the experiment on Heroku."""
        sandbox_output = subprocess.check_output(
            "cd examples/bartlett1932; wallace sandbox --verbose", shell=True)

        id = re.search(
            'Running as experiment (.*)...', sandbox_output).group(1)

        r = requests.get("http://{}.herokuapp.com/summary".format(id))

        assert r.json()['status'] == []

        subprocess.call(
            "heroku apps:destroy --app {} --confirm {}".format(id),
            shell=True)

"""Tests for the Wallace API."""

import subprocess
import re
import os
import requests


class TestHeroku(object):

    """The Heroku test class."""

    sandbox_output = subprocess.check_output(
        "cd examples/bartlett1932; wallace sandbox --verbose", shell=True)

    os.environ['app_id'] = re.search(
        'Running as experiment (.*)...', sandbox_output).group(1)

    @classmethod
    def teardown_class(cls):
        """Remove the app from Heroku."""
        app_id = os.environ['app_id']
        subprocess.call(
            "heroku apps:destroy --app {} --confirm {}".format(app_id, app_id),
            shell=True)

    def test_summary(self):
        """Launch the experiment on Heroku."""
        app_id = os.environ['app_id']
        r = requests.get("http://{}.herokuapp.com/summary".format(app_id))
        assert r.json()['status'] == []

Refactor Heroku tests to have shared setup
Refactor Heroku tests to have shared setup

  """Tests for the Wallace API."""
  
  import subprocess
  import re
+ import os
  import requests
  
  
  class TestHeroku(object):
  
      """The Heroku test class."""
  
+     sandbox_output = subprocess.check_output(
+         "cd examples/bartlett1932; wallace sandbox --verbose", shell=True)
+ 
+     os.environ['app_id'] = re.search(
+         'Running as experiment (.*)...', sandbox_output).group(1)
+ 
+     @classmethod
+     def teardown_class(cls):
+         """Remove the app from Heroku."""
+         app_id = os.environ['app_id']
+         subprocess.call(
+             "heroku apps:destroy --app {} --confirm {}".format(app_id, app_id),
+             shell=True)
+ 
-     def test_sandbox(self):
+     def test_summary(self):
          """Launch the experiment on Heroku."""
+         app_id = os.environ['app_id']
-         sandbox_output = subprocess.check_output(
-             "cd examples/bartlett1932; wallace sandbox --verbose", shell=True)
- 
-         id = re.search(
-             'Running as experiment (.*)...', sandbox_output).group(1)
- 
-         r = requests.get("http://{}.herokuapp.com/summary".format(id))
+         r = requests.get("http://{}.herokuapp.com/summary".format(app_id))
- 
          assert r.json()['status'] == []
  
-         subprocess.call(
-             "heroku apps:destroy --app {} --confirm {}".format(id),
-             shell=True)
- 
Refactor Heroku tests to have shared setup
## Code Before:
"""Tests for the Wallace API."""

import subprocess
import re
import requests


class TestHeroku(object):

    """The Heroku test class."""

    def test_sandbox(self):
        """Launch the experiment on Heroku."""
        sandbox_output = subprocess.check_output(
            "cd examples/bartlett1932; wallace sandbox --verbose", shell=True)

        id = re.search(
            'Running as experiment (.*)...', sandbox_output).group(1)

        r = requests.get("http://{}.herokuapp.com/summary".format(id))

        assert r.json()['status'] == []

        subprocess.call(
            "heroku apps:destroy --app {} --confirm {}".format(id),
            shell=True)

## Instruction:
Refactor Heroku tests to have shared setup
## Code After:
"""Tests for the Wallace API."""

import subprocess
import re
import os
import requests


class TestHeroku(object):

    """The Heroku test class."""

    sandbox_output = subprocess.check_output(
        "cd examples/bartlett1932; wallace sandbox --verbose", shell=True)

    os.environ['app_id'] = re.search(
        'Running as experiment (.*)...', sandbox_output).group(1)

    @classmethod
    def teardown_class(cls):
        """Remove the app from Heroku."""
        app_id = os.environ['app_id']
        subprocess.call(
            "heroku apps:destroy --app {} --confirm {}".format(app_id, app_id),
            shell=True)

    def test_summary(self):
        """Launch the experiment on Heroku."""
        app_id = os.environ['app_id']
        r = requests.get("http://{}.herokuapp.com/summary".format(app_id))
        assert r.json()['status'] == []
