import os
import sys
import unittest


def suite():
    MODULE_DIR = os.path.join(os.path.dirname(__file__), '..')
    MODULE_DIR = os.path.abspath(MODULE_DIR)
    sys.path.insert(0, MODULE_DIR)
    sys.path.insert(0, os.path.dirname(__file__))

    SUB_UNITS = os.path.dirname(__file__)
    SUB_UNITS = os.listdir(SUB_UNITS)
    SUB_UNITS = [
        filename[:-3]
        for filename in SUB_UNITS
        if filename.startswith('test_')
    ]

    os.chdir(os.path.dirname(__file__))
    loader = unittest.TestLoader()
    return loader.loadTestsFromNames(SUB_UNITS)

from os import walk, chdir
from os.path import join, dirname, splitext, abspath, relpath
import sys
import unittest

MODULE_DIR = join(dirname(__file__), '..')
MODULE_DIR = abspath(MODULE_DIR)


def walker(opath='.'):
    for path, folders, files in walk(opath):
        for filename in files:
            if filename.startswith('test_') and filename.endswith('.py'):
                rpath = relpath(path, opath)

                yield (rpath + '.' + splitext(filename)[0]).strip('.')


def suite():
    sys.path.insert(0, MODULE_DIR)
    sys.path.insert(0, dirname(__file__))

    SUB_UNITS = dirname(__file__)
    SUB_UNITS = walker(SUB_UNITS)

    chdir(dirname(__file__))

    return unittest.TestLoader().loadTestsFromNames(SUB_UNITS)

Rework for tests in subdirectories
Rework for tests in subdirectories

- import os
+ from os import walk, chdir
+ from os.path import join, dirname, splitext, abspath, relpath
  import sys
  import unittest
  
+ MODULE_DIR = join(dirname(__file__), '..')
+ MODULE_DIR = abspath(MODULE_DIR)
+ 
+ 
+ def walker(opath='.'):
+     for path, folders, files in walk(opath):
+         for filename in files:
+             if filename.startswith('test_') and filename.endswith('.py'):
+                 rpath = relpath(path, opath)
+ 
+                 yield (rpath + '.' + splitext(filename)[0]).strip('.')
+ 
  
  def suite():
-     MODULE_DIR = os.path.join(os.path.dirname(__file__), '..')
-     MODULE_DIR = os.path.abspath(MODULE_DIR)
      sys.path.insert(0, MODULE_DIR)
-     sys.path.insert(0, os.path.dirname(__file__))
+     sys.path.insert(0, dirname(__file__))
  
-     SUB_UNITS = os.path.dirname(__file__)
+     SUB_UNITS = dirname(__file__)
-     SUB_UNITS = os.listdir(SUB_UNITS)
+     SUB_UNITS = walker(SUB_UNITS)
-     SUB_UNITS = [
-         filename[:-3]
-         for filename in SUB_UNITS
-         if filename.startswith('test_')
-     ]
  
-     os.chdir(os.path.dirname(__file__))
+     chdir(dirname(__file__))
-     loader = unittest.TestLoader()
-     return loader.loadTestsFromNames(SUB_UNITS)
  
+     return unittest.TestLoader().loadTestsFromNames(SUB_UNITS)
+ 
Rework for tests in subdirectories
## Code Before:
import os
import sys
import unittest


def suite():
    MODULE_DIR = os.path.join(os.path.dirname(__file__), '..')
    MODULE_DIR = os.path.abspath(MODULE_DIR)
    sys.path.insert(0, MODULE_DIR)
    sys.path.insert(0, os.path.dirname(__file__))

    SUB_UNITS = os.path.dirname(__file__)
    SUB_UNITS = os.listdir(SUB_UNITS)
    SUB_UNITS = [
        filename[:-3]
        for filename in SUB_UNITS
        if filename.startswith('test_')
    ]

    os.chdir(os.path.dirname(__file__))
    loader = unittest.TestLoader()
    return loader.loadTestsFromNames(SUB_UNITS)

## Instruction:
Rework for tests in subdirectories
## Code After:
from os import walk, chdir
from os.path import join, dirname, splitext, abspath, relpath
import sys
import unittest

MODULE_DIR = join(dirname(__file__), '..')
MODULE_DIR = abspath(MODULE_DIR)


def walker(opath='.'):
    for path, folders, files in walk(opath):
        for filename in files:
            if filename.startswith('test_') and filename.endswith('.py'):
                rpath = relpath(path, opath)

                yield (rpath + '.' + splitext(filename)[0]).strip('.')


def suite():
    sys.path.insert(0, MODULE_DIR)
    sys.path.insert(0, dirname(__file__))

    SUB_UNITS = dirname(__file__)
    SUB_UNITS = walker(SUB_UNITS)

    chdir(dirname(__file__))

    return unittest.TestLoader().loadTestsFromNames(SUB_UNITS)
