Tying to set up a simple test suite, I have src/main.py and src/tests.py. I am getting this error.
File "main.py", line 26
def error(msg: str) -> int:
^
SyntaxError: invalid syntax
My code is
from main import error
from typing import *
import unittest
class TestMainFunctions(unittest.TestCase):
def test_error():
result = error("e")
self.assertEqual(result, 0)
unittest.main()
Is there any way to fix this? The error() function works fine when running main.py. Is the unittest module just not compatible with typing? If so, how do I get around this?
Related
I am trying to unit test Python based aws lambda code. I tried to use patch to mock environment variables but it throws error.How to handle global variables during unit testing. Any help is appreciated
Tried #mock #patch but nothing seems to be working. When executing the testcase the values seems to be empty
[registration.py]
import os
#Global Variables
DEBUG = os.environ.get("EnableLog")
rest_endpoint = os.environ.get('restApiEndpoint')
db_fn_endpoint = rest_endpoint+":3000/rpc/"
def lambda_handler(event,context):
return "success" #to see if this works
if __name__ == "__main__":
lambda_handler('', '')
Unit Test [test_registration.py]
import json
import pytest
import sys, os
from mock import patch
from businesslogic import registration
#mock.patch.dict(os.environ,{'EnableLog':'True'})
#mock.patch.dict(os.environ,{'restApiEndpoint':'http://localhost'})
#patch('registration.restApiEndpoint', 'http://localhost')
def test_lambda_handler():
assert lambda_handler() =="success"
When I run pytest test_registration.py
I get below exception
businesslogic\registration.py:6: in <module>
db_fn_endpoint = rest_endpoint+":3000/rpc/"
E TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
You can pass it as restApiEndpoint=http://localhost python -m pytest
OR
you if you are using sh file to run export before running test
export restApiEndpoint=http://localhost
I am trying to import class called storePass() from test2 into test
I have done this->
test1->
import smtplib
from test2 import storePass
Gmail = storePass()
a = Gmail.returnPass()
test2->
class storePass():
Gmail_pass = "xcmsijw19021"
def returnPass(self):
return self.Gmail_pass
However I am getting the following error ->
TypeError: returnPass() takes 0 positional arguments but 1 was given
When I try to write the code as follows ->
class storePass():
Gmail_pass = "xcmsijw19021"
def returnPass(self):
return self.Gmail_pass
Gmail = storePass()
a = Gmail.returnPass()
I am getting no errors and I can execute print(a) without any problem.
So It's certainly something wrong with my import !
EDIT : Both test1 and test2 are in same directory !
I just tried to run your code and it works on my python 2.7.6 interpreter.
I print a and it gives me as a result xcmsijw19021.
This is the code I used:
file.py
import smtplib
from test2 import storePass
Gmail = storePass()
a = Gmail.returnPass()
print a
test2.py
class storePass():
Gmail_pass = "xcmsijw19021"
def returnPass(self):
return self.Gmail_pass
Then I did run python file.py and it prints me correctly the output.
I think it's some interpreter/ide problem maybe?
Python3 works aswell, by using print(a)
i tried and ran it on Ubuntu 16.04 python 2.7 and it worked as expected. make sure your two files are at the same folder or that the test2 path is in PYTHONPATH.
anyway to F.Leone python does not have a compiler and from what i know it does not depend on specific IDE.
I have two modules misc.py and main.py and would like to define all classes present in misc.py in main.py.
Below is the code
#misc.py
class dummy:
def __init__(self):
pass
def dummyPrint(self):
print "Welcome to python"
#main.py
import misc
dummyObj = dummy()
dummyObj.dummyPrint()
Is this the right way to go ? I do not see any output i.e., Welcome to python
$python misc_main.py misc.py
EDIT: I added the statement from misc import dummy and i am getting the following error
$python misc_main.py main.py
Traceback (most recent call last):
File "misc_main.py", line 5, in <module>
dummyObj = dummmy()
NameError: name 'dummmy' is not defined
When you do the following command, you are calling misc_main.py from the interpreter with misc.py as an argument.
python misc_main.py misc.py
Since misc_main is not reading command line arguments, this is equivalent to
python misc_main.py
I am surprised that you do not get errors, in either case. You need to import the actual class if you want to get output.
from misc import dummy
dummyObj = dummy()
dummyObj.dummyPrint()
Note, I am assuming your main file is actually in called misc_main.py rather than main.py as you have stated in your question. Otherwise you are not invoking the correct file.
I've got the following tests.py file:
from django.test import TestCase
from lxml import etree
import tempfile
import utils
class CreateSurveyFromCsvTextTests(TestCase):
def parsesSurveyPassedInAsCsvAndReturnsXmlRepresentation(self):
text = """"survey",,,,,
,"name","type","label","hint","required"
,"gps","geopoint","Record your current location",,"false"
,"start","start",,,
,"end","end",,,
"settings",
,"form_title"
,"New survey" """
xml = create_survey_from_csv_text(text)
lxml.fromstring(xml)
when I run my module with python manage.py test, the output is
Ran 0 tests in 0.000s
I know the runner is picking up the file because if I make an invalid import it throws an error.
Why is the test not being called?
The name of the test methods need to start with test_. This allows the class to have both test methods and helper methods that you may write as well.
Hence you should rename your method to test_parsesSurveyPassedInAsCsvAndReturnsXmlRepresentation (and perhaps shorten the name too).
UPDATE: If i change
from scitools.std import *
to e.g.
from scitools.std import sqrt, zeros
everything works fine..
I'm trying to run nosestests -s myfile.py, but I'm getting this error all the time:
======================================================================
ERROR: Test if modulename can be imported, and if not, write
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/nose/case.py", line 197, in runTest
self.test(*self.arg)
File "/usr/local/lib/python2.7/dist-packages/nose/util.py", line 613, in newfunc
return func(*arg, **kw)
TypeError: test_if_module_exists() takes at least 1 argument (0 given)
PROGRAM:
from scitools.std import *
import nose.tools as nt
def test_test():
diff = 1E-6
nt.assert_almost_equal(diff, 0, delta=1E-5)
def main():
print __name__
if __name__ == "__main__":
main()
I'm running Nose 1.3.0. Have searched all over the internet for solutions, can't find a thing!
Thanks guys!
Because you're using a wildcard import... Dont use wildcards!
Basically when you say
from scitools.stf import *
You are also importing everything from:
scitools.easyviz
scitools.basics
The way that nose works is that it looks for all the functions named test_ in your module. That includes every function you wrote and every function that you imported. This is called Duck typing. So if there is a function you do not want nose to try to run, Dont import it. When you were using a a wildcard import, you are importing everything, which is most of why wildcards are not a good idea to use. Just import the functions you need, eg
from scitools.std import sqrt, zeros