dimanche 1 décembre 2019

How to continually run tests on save with Python3?

I've tried a number of solutions including nose, nose2, nosey, nose-watch and none of them are running my tests. Hopefully it's something obvious just missing to me.

I can run my (two) tests successfully with

python test_1.py

..
----------------------------------------------------------------------
Ran 2 tests in 2.002s

OK

But when I try to run it continually with the other options I get the messages below. Maybe its because I am using Python3 and many of the solutions might be old and Python2 perhaps.

Does anyone have either a fix for this or another working solution to run tests when I ctrl-save files - both test and app files within my dirdectory tree ?

Errors:

This might be good but it doesn't find the tests...

$ nosetests


Ran 0 tests in 0.000s

OK


This output is quite confusing

$ nose2                                                                                                                                    

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

OK                                                                                                                                         
E                                                                                                                                          
======================================================================                                                                     
ERROR: test_1 (nose2.loader.ModuleImportFailure)                                                                                           
----------------------------------------------------------------------                                                                     
ImportError: Failed to import test module: test_1                                                                                          
Traceback (most recent call last):                                                                                                         
  File "/home/durrantm/.local/lib/python3.7/site-packages/nose2/plugins/loader/discovery.py", line 201, in _find_tests_in_file             
    module = util.module_from_name(module_name)                                                                                            
  File "/home/durrantm/.local/lib/python3.7/site-packages/nose2/util.py", line 77, in module_from_name                                     
    __import__(name)                                                                                                                       
  File "/home/durrantm/Dropbox/90_2019/work/code/pair_and_mob/python/for_nose_2/test_1.py", line 14, in <module>                           
    unittest.main()
  File "/usr/lib/python3.7/unittest/main.py", line 101, in __init__
    self.runTests()
  File "/usr/lib/python3.7/unittest/main.py", line 273, in runTests
    sys.exit(not self.result.wasSuccessful())
SystemExit: False


----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)

This didn't work either

$ nosetests --with-watch 
Usage: nosetests [options]

nosetests: error: no such option: --with-watch

My test code:

# test_1.py
import unittest
from mycode import *
import time
class MyFirstTests(unittest.TestCase):
  def test_hello(self):
    time.sleep(1)
    self.assertEqual(hello_world(), 'hello world')
  def test_goodbye(self):
    time.sleep(1)
    self.assertEqual(goodbye_world(), 'goodbye world')
time.sleep(1)
unittest.main()
The app code
# mycode.py
$ cat mycode.py
def hello_world():
  return 'hello world'
def goodbye_world():
  return 'goodbye world'

I'm on Linux (Ubuntu19.04) but I doubt this is OS specific.

Aucun commentaire:

Enregistrer un commentaire