I have 2 files:
click_example.py
import click
#click.group(invoke_without_command=True)
#click.option('--apikey', default='My key',
help="API key to use.")
#click.pass_context
def main(ctx, apikey):
"""
A CLI for live and past football scores from various football leagues.
resources are given as commands and subresources and their filters as options
"""
headers = {'X-Auth-Token': apikey}
ctx.obj['headers'] = headers
#main.command()
#click.option('--limit', '-l', help='limit number of records')
def scorers(limit):
click.echo('limit is : %s' % limit)
if __name__ == '__main__':
main(obj={})
and a test file:
test_cs.py
from click.testing import CliRunner
import unittest
from .clicksample import main
class Sample(unittest.TestCase):
def setUp(self):
self.runner = CliRunner()
def tearDown(self):
pass
def test_sample_command(self):
result = self.runner.invoke(main)
self.assertEqual(0, result.exit_code)
if __name__ == '__main__':
unittest.main()
I want to know why the test does not pass. When I run the clicksample script from the command line it works as expected but for some reason it does not exit as expected in the test. When I poke the result using pdb I get the following stats:
(Pdb) result
<Result TypeError("'NoneType' object does not support item assignment",)>
(Pdb) result.exit_code
-1
(Pdb)
You never set ctx.obj. This can fix that for you:
def main(ctx, apikey):
if ctx.obj is None:
ctx.obj = {}
...
Related
I'm using click v8.1.3 and I'm trying to create some pytests, but I'm not getting my expected return_value when using click.testing.CliRunner().invoke
import click.testing
import mycli
def test_return_ctx():
#mycli.cli.command()
def foo():
return "Potato"
runner = click.testing.CliRunner()
result = runner.invoke(mycli.cli, ["foo"])
assert result.return_value == "Potato" # this fails. b/c the actual value is None
I tried updating the root command to return some random value as well to see if we get a value there
# mycli
import click
#click.group()
def cli():
return "Potato"
But it didn't help. return_value for the Result object is still None
Am I misunderstanding how I should return a value from a command?
https://click.palletsprojects.com/en/8.1.x/api/#click.testing.Result.return_value
Click command handlers do not return a value unless you use: standalone_mode=False. You can do that during testing like:
result = CliRunner().invoke(foo, standalone_mode=False)
Test code:
import click
from click.testing import CliRunner
def test_return_value():
#click.command()
def foo():
return "bar"
result = CliRunner().invoke(foo, standalone_mode=False)
assert result.return_value == "bar"
Test Results:
============================= test session starts ============================
collecting ... collected 1 item
test_code.py::test_return_value PASSED [100%]
============================== 1 passed in 0.03s ==============================
I am trying to rerun my python script every 10 seconds. I have tried various things with the time.sleep function but nothing seems to be working. I have tried different variants of time.sleep:
def job()
# script
if __name__ == '__main__':
while True:
job()
time.sleep(10)
but not finding the correct place/way to implement it. I don't want to rerun a function, but the full code.
This is the script I am trying to rerun:
import...
def main():
# parse the command line arguments
args = ConfigArgumentParser(description=__doc__).parse_args()
if _debug: _log.debug("initialization")
if _debug: _log.debug(" - args: %r", args)
# make a device object
this_device = LocalDeviceObject(
objectName=args.ini.objectname,
objectIdentifier=('device', int(args.ini.objectidentifier)),
maxApduLengthAccepted=int(args.ini.maxapdulengthaccepted),
segmentationSupported=args.ini.segmentationsupported,
vendorIdentifier=int(args.ini.vendoridentifier),
)
# make a sample application
this_application = BIPSimpleApplication(this_device, args.ini.address)
# make some random input objects
for i in range(1, RANDOM_OBJECT_COUNT + 1):
ravo = RandomAnalogValueObject(
objectIdentifier=('analogValue', i),
objectName='Temp%d' % (i,),
)
this_application.add_object(ravo)
# make sure they are all there
_log.debug(" - object list: %r", this_device.objectList)
_log.debug("running")
run()
_log.debug("fini")
if __name__ == "__main__":
main()
If you need the whole thing to run (even bootstrap code), you can use a shell script to do it.
This won't refresh environment variables though.
#!/usr/bin/env bash
while true; do
sleep 10
python script.py
done
If you need to refresh environment variables (you seem to need it because of RANDOM_OBJECTS), add eval "$(exec /usr/bin/env -i "${SHELL}" -l -c "export")" to the mix. Source: https://unix.stackexchange.com/a/581684
Try this:
if __name__ == "__main__":
time.sleep(10)
main()
What you can do is just restart your whole script after 10s.
Applied to your code it would look like this:
import os
import time
import json
import sys
from bacpypes.debugging import bacpypes_debugging, ModuleLogger
from bacpypes.consolelogging import ConfigArgumentParser
from bacpypes.core import run
from bacpypes.primitivedata import Real
from bacpypes.object import AnalogValueObject, Property, register_object_type
from bacpypes.errors import ExecutionError
from bacpypes.app import BIPSimpleApplication
from bacpypes.local.device import LocalDeviceObject
# Read Json
with open('ObjectList.json') as json_file:
data = json.load(json_file)
def get_present_value(no):
for a in data['AnalogValues']:
if a['ObjectIdentifier'] == int(no):
return a['PresentValue']
return None
ai_1 = (get_present_value(1))
print(ai_1)
# some debugging
_debug = 0
_log = ModuleLogger(globals())
# settings
RANDOM_OBJECT_COUNT = int(os.getenv('RANDOM_OBJECT_COUNT', 1))
#
# RandomValueProperty
#
class RandomValueProperty(Property):
def __init__(self, identifier):
if _debug: RandomValueProperty._debug("__init__ %r", identifier)
Property.__init__(self, identifier, Real, default=0.0, optional=True, mutable=False)
def ReadProperty(self, obj, arrayIndex=None):
if _debug: RandomValueProperty._debug("ReadProperty %r arrayIndex=%r", obj, arrayIndex)
# access an array
if arrayIndex is not None:
raise ExecutionError(errorClass='property', errorCode='propertyIsNotAnArray')
# return a random value
value = ai_1
if _debug: RandomValueProperty._debug(" - value: %r", value)
return value
def WriteProperty(self, obj, value, arrayIndex=None, priority=None, direct=False):
if _debug: RandomValueProperty._debug("WriteProperty %r %r arrayIndex=%r priority=%r direct=%r", obj, value,
arrayIndex, priority, direct)
raise ExecutionError(errorClass='property', errorCode='writeAccessDenied')
bacpypes_debugging(RandomValueProperty)
#
# Random Value Object Type
#
class RandomAnalogValueObject(AnalogValueObject):
properties = [
RandomValueProperty('presentValue'),
]
def __init__(self, **kwargs):
if _debug: RandomAnalogValueObject._debug("__init__ %r", kwargs)
AnalogValueObject.__init__(self, **kwargs)
bacpypes_debugging(RandomAnalogValueObject)
register_object_type(RandomAnalogValueObject)
#
# __main__
#
def main():
# parse the command line arguments
args = ConfigArgumentParser(description=__doc__).parse_args()
if _debug: _log.debug("initialization")
if _debug: _log.debug(" - args: %r", args)
# make a device object
this_device = LocalDeviceObject(
objectName=args.ini.objectname,
objectIdentifier=('device', int(args.ini.objectidentifier)),
maxApduLengthAccepted=int(args.ini.maxapdulengthaccepted),
segmentationSupported=args.ini.segmentationsupported,
vendorIdentifier=int(args.ini.vendoridentifier),
)
# make a sample application
this_application = BIPSimpleApplication(this_device, args.ini.address)
# make some random input objects
for i in range(1, RANDOM_OBJECT_COUNT + 1):
ravo = RandomAnalogValueObject(
objectIdentifier=('analogValue', i),
objectName='Temp%d' % (i,),
)
this_application.add_object(ravo)
# make sure they are all there
_log.debug(" - object list: %r", this_device.objectList)
_log.debug("running")
run()
_log.debug("fini")
if __name__ == "__main__":
main()
time.sleep(10)
os.execl(sys.executable, sys.executable, * sys.argv) # Your script restarts after 10s with this
I have the below code:
master.py
def create():
master = falcon.API(middleware=Auth())
msg = Message()
master.add_route('/message', msg)
master = create()
if __name__ == '__main__':
httpd = simple_server.make_server("127.0.0.1", 8989, master)
process = Thread(target=httpd.serve_forever, name="master_process")
process.start()
#Some logic happens here
s_process = Thread(target=httpd.shutdown(), name="shut_process")
s_process.start()
s.join()
I tried to create the below test case for the below:
from falcon import testing
from master import create
#pytest.fixture(scope='module')
def client():
return testing.TestClient(create())
def test_post_message(client):
result = client.simulate_post('/message', headers={'token': "ubxuybcwe"}, body='{"message": "I'm here!"}') --> This line throws the error
assert result.status_code == 200
I tried running the above but get the below error:
TypeError: 'NoneType' object is not callable
I actually am unable to figure out how I should go about writing the test case for this.
Based on what #hoefling said, the below fixed it:
master.py
def create():
master = falcon.API(middleware=Auth())
msg = Message()
master.add_route('/message', msg)
return master
master = create()
if __name__ == '__main__':
httpd = simple_server.make_server("127.0.0.1", 8989, master)
process = Thread(target=httpd.serve_forever, name="master_process")
process.start()
#Some logic happens here
s_process = Thread(target=httpd.shutdown(), name="shut_process")
s_process.start()
s.join()
And then the test case works:
from falcon import testing
from master import create
#pytest.fixture(scope='module')
def client():
return testing.TestClient(create())
def test_post_message(client):
result = client.simulate_post('/message', headers={'token': "ubxuybcwe"},
body='{"message": "I'm here!"}')
assert result.status_code == 200
Thanks a lot #hoefling!
Appium automation runs and ends correctly inside one class, but fails when another class is created.
I tried to to delete all attributes in init, except first one and it helps.
Main Class(works well)
import unittest
from appium import webdriver
from ScreenObjects.SendMessage import SendMsg
class AppTestAppium(unittest.TestCase):
def setUp(self):
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = ''
desired_caps['deviceName'] = '81e9b20e'
desired_caps['appPackage'] = 'org.telegram.messenger'
desired_caps['appActivity'] = 'org.telegram.ui.LaunchActivity'
desired_caps['noReset'] = 'True'
self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_caps)
def tearDown(self):
"Tear down the test"
self.driver.quit()
def test_send_messge_to_olga(self):
"Test is sending message"
contactItem = self.driver.find_element_by_xpath("//android.view.ViewGroup[contains(#index,'2')]")
contactItem.click()
elementTypeField = self.driver.find_element_by_xpath("//android.widget.EditText[contains(#index,'1')]")
elementTypeField.clear()
elementTypeField.send_keys("Hello. If you are reading this, my first appium automation has passed")
sendButton = self.driver.find_element_by_xpath("//android.widget.ImageView[#content-desc ='Send']")
sendButton.click()
if __name__ == '__main__':
unittest.main()
Main class(fails)
import unittest
from appium import webdriver
from ScreenObjects.SendMessage import SendMsg
class AppTestAppium(unittest.TestCase):
def setUp(self):
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = ''
desired_caps['deviceName'] = '81e9b20e'
desired_caps['appPackage'] = 'org.telegram.messenger'
desired_caps['appActivity'] = 'org.telegram.ui.LaunchActivity'
desired_caps['noReset'] = 'True'
self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_caps)
def tearDown(self):
"Tear down the test"
self.driver.quit()
def test_send_messge_to_olga(self):
"Test is sending message"
send_message = SendMsg(self.driver)
send_message.chooseContact()
send_message.typeMsg()
send_message.clickSend()
if __name__ == '__main__':
unittest.main()
with one more Class (fails)
class SendMsg(object):
def __init__(self, driver):
self.contactItem = driver.find_element_by_xpath("//android.view.ViewGroup[contains(#index,'2')]")
self.elementTypeField = driver.find_element_by_xpath("//android.widget.EditText[contains(#index,'1')]")
self.sendButton = driver.find_element_by_xpath("//android.widget.ImageView[#content-desc ='Send']")
def chooseContact(self):
self.contactItem.click()
def typeMsg(self):
self.elementTypeField.clear()
self.elementTypeField.send_keys("Hello")
def clickSend(self):
self.sendButton.click()
I expect TEST PASSED, but the actual output is 'An element could not be located on the page using the given search parameters.'
I have to test few sites which sales the same things, but they have an another template.
So I want to run each MainTestClass with giving some input parameter, let's say :
java -jar SeleniumServerStandalone-2.0b2.jar -port 5555 (template_id=5)
Is it possible?
class MainTestCases(unittest.TestCase):
def setUp(self):
#self.template_id=template_id I want something like that
self.verificationErrors = []
self.selenium = selenium("localhost", 5555, "*chrome", "http://www.google.com/")
time.sleep(5)
self.selenium.start()
def test_test1(self):
if self.template_id==1:
...
elif self.template_id==2:
...
def test_test2(self):
if self.template_id==1:
...
elif self.template_id==2:
...
def tearDown(self):
self.selenium.stop()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
Try adding an init method to MainTestCases, like so:
class MainTestCases(unittest.TestCase):
def __init__(self, methodName, template_id):
super(MainTestCases, self).__init__(self, methodName)
self.template_id = templateId
def setUp(self):
... and so on...
Due to this customization, you will need to build your test suite manually because each test case has to be instantiated with the template_id, like so--
def suite(template_id):
testcasenames = unittest.defaultTestLoader.loadTestsFromTestCase(MainTestCases)
suite = []
for case in testcasename:
suite.append(MainTestCases(case, template_id)
return suite
Then in main, instead of unittest.main(), do:
Parse command-line arguments. You may want to consider the argparse (2.7+) or optparse (2.6 and earlier) modules. They are powerful, but easy to start with by looking at the examples.
Create and run the suite: unittest.TextTestRunner().run(suite(template_id))
Now, I use this solution:
Create suite test which runs testcases:
import unittest
from Flights.FlightsTestCases import FlightsTestCases
import sys
from Flights.FlightTemplate import FlightTemplate
def suite():
testSuite= unittest.TestSuite()
testSuite.addTest(FlightsTestCases('test_test1'))
FlightsTestCases.www_address='http://testpage.pl/'
FlightsTestCases.flight_template=FlightTemplate.Test
#FlightsTestCases.www_address='http://productionpage.pl/'
#FlightsTestCases.flight_template=FlightTemplate.Production
return testSuite
if __name__ == "__main__":
result = unittest.TextTestRunner(verbosity=2).run(suite())
sys.exit(not result.wasSuccessful())
change set_up to something like:
class FlightsTestCases(unittest.TestCase):
www_address = None
flight_template = None
xml_report_generator = None
def setUp(self):
self.verificationErrors = []
if self.www_address == None:
self.selenium = selenium("localhost", 5555, "*chrome", "http://testpage.pl/")
else:
self.selenium = selenium("localhost", 5555, "*chrome", self.www_address)