nvdv / vprof

Visual profiler for Python
BSD 2-Clause "Simplified" License
3.96k stars 153 forks source link

vprof interfere with unittest autodiscovery #42

Open xcombelle opened 8 years ago

xcombelle commented 8 years ago

With this code:

import unittest

class MyTest(unittest.TestCase):
    def test_fail(self):
        self.assertTrue(False)

unittest.main()

I get the following result

xcombelle@ender ~/d/sgftool> vprof -c cmh  -s test.py  --port 8080
Running RuntimeProfile...

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK
Running MemoryProfile...

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK
Running CodeHeatmapProfile...

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK
Starting HTTP server...
^CStopping...
xcombelle@ender ~/d/sgftool> python3 test.py  --port 8080
usage: test.py [-h] [-v] [-q] [-f] [-c] [-b] [tests [tests ...]]
test.py: error: unrecognized arguments: --port
xcombelle@ender ~/d/sgftool> python3 test.py 
F
======================================================================
FAIL: test_fail (__main__.MyTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test.py", line 5, in test_fail
    self.assertTrue(False)
AssertionError: False is not true

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (failures=1)

Both execution should have the same result

nvdv commented 8 years ago

This is interesting. It seems that it's because unittest does some magic to do autodiscovery. I'll take a look.

nvdv commented 8 years ago

It happens because unittest discovery tries to load tests from __main__ module by default. When module is run under vprof, __main__ points to vprof module, which does not have any tests and that's why line Ran 0 tests in 0.000s is displayed. I can suggest to specify module explicitly unittest.main(module=...) if you need to run it under vprof.

xcombelle commented 8 years ago

That sounds logical thanks for watching

shardool-freightwalla commented 8 years ago

Facing similar issue. Explicitly specifying the module too not working for me.

nvdv commented 8 years ago

Works for me when I save this code to example_test.py:

import unittest

class MyTest(unittest.TestCase):
    def test_fail(self):
        self.assertTrue(False)

unittest.main(module='example_test')

and run it.