import os
import subprocess
import time
import glob
import unittest

class ZxSpecTestCase(unittest.TestCase):
    def setUp(self):
        clean()

    def tearDown(self):
        clean()

    def test_zx_spec_header_displayed(self):
        ZX_SPEC_OUTPUT_FILE = "printout.txt"

        proc = subprocess.Popen([
            "fuse",
            "--tape", "bin/zx-spec-test.tap",
            "--auto-load",
            "--no-autosave-settings"])

        while not os.path.exists(ZX_SPEC_OUTPUT_FILE):
            time.sleep(0.1)

        time.sleep(1)
        proc.kill()

        with open(ZX_SPEC_OUTPUT_FILE, 'r') as f:
            zx_spec_output = f.read()

            self.assertRegexpMatches(zx_spec_output, 'ZX Spec - The TDD Framework')

def clean():
    for f in glob.glob("printout.*"):
        os.remove(f)

if __name__ == '__main__':
    unittest.main()

import os
import subprocess
import time
import glob
import unittest

class ZxSpecTestCase(unittest.TestCase):

    @classmethod
    def setUpClass(self):
        clean()
        self.output = run_zx_spec()

    def test_zx_spec_header_displayed(self):
        self.assertRegexpMatches(self.output, 'ZX Spec - The TDD Framework')

    def test_all_tests_pass(self):
        self.assertRegexpMatches(self.output, 'Pass: 0, Fail: 0')

    @classmethod
    def tearDownClass(self):
        clean()

def clean():
    for f in glob.glob("printout.*"):
        os.remove(f)

def run_zx_spec():
    ZX_SPEC_OUTPUT_FILE = "printout.txt"
    proc = subprocess.Popen([
        "fuse",
        "--tape", "bin/zx-spec-test.tap",
        "--auto-load",
        "--no-autosave-settings"])

    wait_count = 0
    while not os.path.exists(ZX_SPEC_OUTPUT_FILE):
        time.sleep(0.1)
        wait_count += 1
        if wait_count == 20:
            raise 'Output file not produced in time'

    time.sleep(1)
    proc.kill()

    with open(ZX_SPEC_OUTPUT_FILE, 'r') as f:
        return f.read()

if __name__ == '__main__':
    unittest.main()

Add timeout for printout production
Add timeout for printout production

  import os
  import subprocess
  import time
  import glob
  import unittest
  
  class ZxSpecTestCase(unittest.TestCase):
+ 
+     @classmethod
-     def setUp(self):
+     def setUpClass(self):
          clean()
+         self.output = run_zx_spec()
- 
-     def tearDown(self):
-         clean()
  
      def test_zx_spec_header_displayed(self):
-         ZX_SPEC_OUTPUT_FILE = "printout.txt"
+         self.assertRegexpMatches(self.output, 'ZX Spec - The TDD Framework')
  
+     def test_all_tests_pass(self):
+         self.assertRegexpMatches(self.output, 'Pass: 0, Fail: 0')
-         proc = subprocess.Popen([
-             "fuse",
-             "--tape", "bin/zx-spec-test.tap",
-             "--auto-load",
-             "--no-autosave-settings"])
  
+     @classmethod
+     def tearDownClass(self):
+         clean()
-         while not os.path.exists(ZX_SPEC_OUTPUT_FILE):
-             time.sleep(0.1)
- 
-         time.sleep(1)
-         proc.kill()
- 
-         with open(ZX_SPEC_OUTPUT_FILE, 'r') as f:
-             zx_spec_output = f.read()
- 
-             self.assertRegexpMatches(zx_spec_output, 'ZX Spec - The TDD Framework')
  
  def clean():
      for f in glob.glob("printout.*"):
          os.remove(f)
  
+ def run_zx_spec():
+     ZX_SPEC_OUTPUT_FILE = "printout.txt"
+     proc = subprocess.Popen([
+         "fuse",
+         "--tape", "bin/zx-spec-test.tap",
+         "--auto-load",
+         "--no-autosave-settings"])
+ 
+     wait_count = 0
+     while not os.path.exists(ZX_SPEC_OUTPUT_FILE):
+         time.sleep(0.1)
+         wait_count += 1
+         if wait_count == 20:
+             raise 'Output file not produced in time'
+ 
+     time.sleep(1)
+     proc.kill()
+ 
+     with open(ZX_SPEC_OUTPUT_FILE, 'r') as f:
+         return f.read()
+ 
  if __name__ == '__main__':
      unittest.main()
  
Add timeout for printout production
## Code Before:
import os
import subprocess
import time
import glob
import unittest

class ZxSpecTestCase(unittest.TestCase):
    def setUp(self):
        clean()

    def tearDown(self):
        clean()

    def test_zx_spec_header_displayed(self):
        ZX_SPEC_OUTPUT_FILE = "printout.txt"

        proc = subprocess.Popen([
            "fuse",
            "--tape", "bin/zx-spec-test.tap",
            "--auto-load",
            "--no-autosave-settings"])

        while not os.path.exists(ZX_SPEC_OUTPUT_FILE):
            time.sleep(0.1)

        time.sleep(1)
        proc.kill()

        with open(ZX_SPEC_OUTPUT_FILE, 'r') as f:
            zx_spec_output = f.read()

            self.assertRegexpMatches(zx_spec_output, 'ZX Spec - The TDD Framework')

def clean():
    for f in glob.glob("printout.*"):
        os.remove(f)

if __name__ == '__main__':
    unittest.main()

## Instruction:
Add timeout for printout production
## Code After:
import os
import subprocess
import time
import glob
import unittest

class ZxSpecTestCase(unittest.TestCase):

    @classmethod
    def setUpClass(self):
        clean()
        self.output = run_zx_spec()

    def test_zx_spec_header_displayed(self):
        self.assertRegexpMatches(self.output, 'ZX Spec - The TDD Framework')

    def test_all_tests_pass(self):
        self.assertRegexpMatches(self.output, 'Pass: 0, Fail: 0')

    @classmethod
    def tearDownClass(self):
        clean()

def clean():
    for f in glob.glob("printout.*"):
        os.remove(f)

def run_zx_spec():
    ZX_SPEC_OUTPUT_FILE = "printout.txt"
    proc = subprocess.Popen([
        "fuse",
        "--tape", "bin/zx-spec-test.tap",
        "--auto-load",
        "--no-autosave-settings"])

    wait_count = 0
    while not os.path.exists(ZX_SPEC_OUTPUT_FILE):
        time.sleep(0.1)
        wait_count += 1
        if wait_count == 20:
            raise 'Output file not produced in time'

    time.sleep(1)
    proc.kill()

    with open(ZX_SPEC_OUTPUT_FILE, 'r') as f:
        return f.read()

if __name__ == '__main__':
    unittest.main()
