from py101 import Story
from py101 import variables
from py101 import lists
import unittest


class TestStory(unittest.TestCase):
    def test_name(self):
        self.assertEqual(Story().name, 'py101', "name should be py101")


class TestAdventureVariables(unittest.TestCase):
    good_solution = """
myinteger = 4
mystring = 'Python String Here'
print(myinteger)
print(mystring)
    """

    def test_solution(self):
        test = variables.TestOutput(self.good_solution)
        test.setUp()
        try:
            test.runTest()
        finally:
            test.tearDown()


class TestAdventureLists(unittest.TestCase):
    good_solution = """
languages = ["ADA", "Pascal", "Fortran", "Smalltalk"]
print(languages)
    """

    def test_solution(self):
        test = lists.TestOutput(self.good_solution)
        test.setUp()
        try:
            test.runTest()
        finally:
            test.tearDown()


import py101
import py101.boilerplate
import py101.introduction
import py101.lists
import py101.variables
import unittest


class TestStory(unittest.TestCase):
    def test_name(self):
        self.assertEqual(py101.Story().name, 'py101', "name should be py101")


class AdventureData(object):
    def __init__(self, test_module, good_solution):
        self.module = test_module
        self.good_solution = good_solution


class TestAdventures(unittest.TestCase):
    adventures = [
        AdventureData(
            py101.boilerplate,
            ""
        ),
        AdventureData(
            py101.introduction,
            """print('Hello World')"""
        ),
        AdventureData(
            py101.variables,
            """myinteger = 4; mystring = 'Python String Here'; print(myinteger); print(mystring)"""
        ),
        AdventureData(
           py101.lists,
           """languages = ["ADA", "Pascal", "Fortran", "Smalltalk"]; print(languages)"""
        )
    ]

    def test_solution(self):
        for adventure in self.adventures:
            with self.subTest(adventure=adventure.module.__name__):
                test = adventure.module.TestOutput(adventure.good_solution)
                test.setUp()
                try:
                    test.runTest()
                finally:
                    test.tearDown()

Refactor tests to remove duplicate code
Refactor tests to remove duplicate code

- from py101 import Story
- from py101 import variables
- from py101 import lists
+ import py101
+ import py101.boilerplate
+ import py101.introduction
+ import py101.lists
+ import py101.variables
  import unittest
  
  
  class TestStory(unittest.TestCase):
      def test_name(self):
-         self.assertEqual(Story().name, 'py101', "name should be py101")
+         self.assertEqual(py101.Story().name, 'py101', "name should be py101")
  
  
+ class AdventureData(object):
+     def __init__(self, test_module, good_solution):
+         self.module = test_module
+         self.good_solution = good_solution
+ 
+ 
- class TestAdventureVariables(unittest.TestCase):
+ class TestAdventures(unittest.TestCase):
-     good_solution = """
- myinteger = 4
- mystring = 'Python String Here'
- print(myinteger)
- print(mystring)
-     """
+     adventures = [
+         AdventureData(
+             py101.boilerplate,
+             ""
+         ),
+         AdventureData(
+             py101.introduction,
+             """print('Hello World')"""
+         ),
+         AdventureData(
+             py101.variables,
+             """myinteger = 4; mystring = 'Python String Here'; print(myinteger); print(mystring)"""
+         ),
+         AdventureData(
+            py101.lists,
+            """languages = ["ADA", "Pascal", "Fortran", "Smalltalk"]; print(languages)"""
+         )
+     ]
  
      def test_solution(self):
-         test = variables.TestOutput(self.good_solution)
+         for adventure in self.adventures:
+             with self.subTest(adventure=adventure.module.__name__):
+                 test = adventure.module.TestOutput(adventure.good_solution)
-         test.setUp()
+                 test.setUp()
-         try:
+                 try:
-             test.runTest()
+                     test.runTest()
-         finally:
+                 finally:
-             test.tearDown()
+                     test.tearDown()
  
- 
- class TestAdventureLists(unittest.TestCase):
-     good_solution = """
- languages = ["ADA", "Pascal", "Fortran", "Smalltalk"]
- print(languages)
-     """
- 
-     def test_solution(self):
-         test = lists.TestOutput(self.good_solution)
-         test.setUp()
-         try:
-             test.runTest()
-         finally:
-             test.tearDown()
- 
- 
Refactor tests to remove duplicate code
## Code Before:
from py101 import Story
from py101 import variables
from py101 import lists
import unittest


class TestStory(unittest.TestCase):
    def test_name(self):
        self.assertEqual(Story().name, 'py101', "name should be py101")


class TestAdventureVariables(unittest.TestCase):
    good_solution = """
myinteger = 4
mystring = 'Python String Here'
print(myinteger)
print(mystring)
    """

    def test_solution(self):
        test = variables.TestOutput(self.good_solution)
        test.setUp()
        try:
            test.runTest()
        finally:
            test.tearDown()


class TestAdventureLists(unittest.TestCase):
    good_solution = """
languages = ["ADA", "Pascal", "Fortran", "Smalltalk"]
print(languages)
    """

    def test_solution(self):
        test = lists.TestOutput(self.good_solution)
        test.setUp()
        try:
            test.runTest()
        finally:
            test.tearDown()


## Instruction:
Refactor tests to remove duplicate code
## Code After:
import py101
import py101.boilerplate
import py101.introduction
import py101.lists
import py101.variables
import unittest


class TestStory(unittest.TestCase):
    def test_name(self):
        self.assertEqual(py101.Story().name, 'py101', "name should be py101")


class AdventureData(object):
    def __init__(self, test_module, good_solution):
        self.module = test_module
        self.good_solution = good_solution


class TestAdventures(unittest.TestCase):
    adventures = [
        AdventureData(
            py101.boilerplate,
            ""
        ),
        AdventureData(
            py101.introduction,
            """print('Hello World')"""
        ),
        AdventureData(
            py101.variables,
            """myinteger = 4; mystring = 'Python String Here'; print(myinteger); print(mystring)"""
        ),
        AdventureData(
           py101.lists,
           """languages = ["ADA", "Pascal", "Fortran", "Smalltalk"]; print(languages)"""
        )
    ]

    def test_solution(self):
        for adventure in self.adventures:
            with self.subTest(adventure=adventure.module.__name__):
                test = adventure.module.TestOutput(adventure.good_solution)
                test.setUp()
                try:
                    test.runTest()
                finally:
                    test.tearDown()
