0

I have a test file (.\tests\test_1.py).

api\
    proj1\
    tests\
        test_1.py

And when running

(base) PS C:\Users\xx\Source\api> python -m unittest -v tests.test_1

It got the fowllowing error:

Traceback (most recent call last):
  File "C:\Users\xx\Anaconda3\lib\runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\xx\Anaconda3\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\xx\Anaconda3\lib\unittest\__main__.py", line 18, in <module>
    main(module=None)
  File "C:\Users\xx\Anaconda3\lib\unittest\main.py", line 100, in __init__
    self.parseArgs(argv)
  File "C:\Users\xx\Anaconda3\lib\unittest\main.py", line 147, in parseArgs
    self.createTests()
  File "C:\Users\xx\Anaconda3\lib\unittest\main.py", line 158, in createTests
    self.test = self.testLoader.loadTestsFromNames(self.testNames,
  File "C:\Users\xx\Anaconda3\lib\unittest\loader.py", line 220, in loadTestsFromNames
    suites = [self.loadTestsFromName(name, module) for name in names]
  File "C:\Users\xx\Anaconda3\lib\unittest\loader.py", line 220, in <listcomp>
    suites = [self.loadTestsFromName(name, module) for name in names]
  File "C:\Users\xx\Anaconda3\lib\unittest\loader.py", line 154, in loadTestsFromName
    module = __import__(module_name)
ValueError: source code string cannot contain null bytes

However, it doesn't have any error when running

(base) PS C:\Users\xx\Source\api> python .\tests\test_1.py

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

OK

The test_1.py file has only the following code now:

from unittest import mock, TestCase, main

class TestDataPipeline(TestCase):
    
    def test_1(self):
        pass
  
if __name__ == '__main__':
    main()
ca9163d9
  • 27,283
  • 64
  • 210
  • 413
  • Maybe a duplicate of https://stackoverflow.com/questions/31233777/python-source-code-string-cannot-contain-null-bytes – Dennis Oct 05 '20 at 05:46
  • I found the question and tried the sed command on my files, but it didn't resolve the problem. Also, if I have null character in the file, why python filename can run it – ca9163d9 Oct 05 '20 at 06:01
  • Where exactly is the null byte? None of the code you posted exhibits any null byte. Voting to close as needs debugging details. – tripleee Oct 05 '20 at 07:17
  • @tripleee, there is no null byte. I don't know how `python -m unittest ...` raise the error. I got the error in a very simple config. Let me know what else debugging details can be helpful. – ca9163d9 Oct 05 '20 at 13:23
  • That sounds like a rather stupid bug in the unittest framework then. Are you _really_ sure? Can you get a more detailed traceback where you can see the problematic value? (I'm thinking of the one you get with iPython and/or cgitb.) – tripleee Oct 05 '20 at 13:32
  • I've copied all the Traceback output of `python -m unittest -v tests.......`. Is it any option to output more infor? My python version is 3.8.3. – ca9163d9 Oct 05 '20 at 13:38
  • I think I found the issue. It's the `__init__.py`. I recreated an empty `__init__.py` and the error is gone. – ca9163d9 Oct 05 '20 at 13:54

0 Answers0