How to test unit tests in Python without adding code

I have a Python project with a bunch of tests that have already been implemented, and I would like to start testing them to compare the performance of code, servers, etc. over time. Finding files in a manner similar to Nose was not a problem, because in any case I have a β€œtest” in the names of all my test files. However, I am trying to run these tests dynamically.

As of now, I can run a script that takes a directory path as an argument and returns a list of file paths, for example:

def getTestFiles(directory):
    fileList = []
    print "Searching for 'test' in " + directory
    if not os.path.isdir(os.path.dirname(directory)):
        # throw error
        raise InputError(directory, "Not a valid directory")
    else:
        for root, dirs, files in os.walk(directory):
            #print files
            for f in files:
                if "test" in f and f.endswith(".py"):
                    fileList.append(os.path.join(root, f))
    return fileList

# returns a list like this:
# [  'C:/Users/myName/Desktop/example1_test.py',
#    'C:/Users/myName/Desktop/example2_test.py',
#    'C:/Users/myName/Desktop/folder1/example3_test.py',
#    'C:/Users/myName/Desktop/folder2/example4_test.py'...  ]

The problem is that these files may have different syntax, which I am trying to figure out how to handle. For instance:

TestExampleOne:

import dummy1
import dummy2
import dummy3

class TestExampleOne(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        # set up

    def test_one(self):
        # test stuff
    def test_two(self):
        # test stuff
    def test_three(self):
        # test stuff

    # etc...

TestExampleTwo:

import dummy1
import dummy2
import dummy3

def setup(self):
    try:
        # config stuff
    except Exception as e:
        logger.exception(e)

def test_one():
    # test stuff
def test_two():
    # test stuff
def test_three():
    # test stuff

# etc...

TestExampleThree:

import dummy1
import dummy2
import dummy3

def setup(self):
    try:
        # config stuff
    except Exception as e:
        logger.exception(e)

class TestExampleTwo(unittest.TestCase):
    def test_one(self):
        # test stuff
    def test_two(self):
        # test stuff
    # etc...

class TestExampleThree(unittest.TestCase):
    def test_one(self):
        # test stuff
    def test_two(self):
        # test stuff
    # etc...

# etc...

, , "test" , unit test , . , - NodeVisitor , . , , .

+4
1

nose test runner , /.

nose-timer :

nosetests, : ?


:

  • , test_nose :

    • test1.py:

      import time
      import unittest
      
      class TestExampleOne(unittest.TestCase):
          @classmethod
          def setUpClass(cls):
              cls.value = 1
      
          def test_one(self):
              time.sleep(1)
              self.assertEqual(1, self.value)
      
    • test2.py:

      import time
      
      value = None
      
      def setup():
          global value
          value = 1
      
      def test_one():
          time.sleep(2)
          assert value == 1
      
    • test3.py:

      import time
      import unittest
      
      value = None
      
      def setup():
          global value
          value = 1
      
      class TestExampleTwo(unittest.TestCase):
          def test_one(self):
              time.sleep(3)
              self.assertEqual(1, value)
      
      class TestExampleThree(unittest.TestCase):
          def test_one(self):
              time.sleep(4)
              self.assertEqual(1, value)
      
  • nose test runner:

    pip install nose
    
  • nose-timer:

    pip install nose-timer
    
  • :

    $ nosetests test_nose --with-timer
    ....
    test_nose.test3.TestExampleThree.test_one: 4.0003s
    test_nose.test3.TestExampleTwo.test_one: 3.0010s
    test_nose.test2.test_one: 2.0011s
    test_nose.test1.TestExampleOne.test_one: 1.0005s
    ----------------------------------------------------------------------
    Ran 4 tests in 10.006s
    
    OK
    

: nosetests results

--timer-ok --timer-warning.

, time.sleep(n) , . , value ​​ 1 , value 1 - .

UPD ( nose nose-timer script):

from pprint import pprint
import nose
from nosetimer import plugin

plugin = plugin.TimerPlugin()
plugin.enabled = True
plugin.timer_ok = 1000
plugin.timer_warning = 2000
plugin.timer_no_color = False


nose.run(plugins=[plugin])
result = plugin._timed_tests
pprint(result)

test.py script :

python test.py /home/example/dir/tests --with-timer

result :

{'test_nose.test1.TestExampleOne.test_one': 1.0009748935699463,
 'test_nose.test2.test_one': 2.0003929138183594,
 'test_nose.test3.TestExampleThree.test_one': 4.000233173370361,
 'test_nose.test3.TestExampleTwo.test_one': 3.001115083694458}
+11

Source: https://habr.com/ru/post/1544051/


All Articles