how to setup a unittest for a web-project - unit-testing

I have problems to properly configure my first unittest in the DartEditor(1.8.5).
I wrote a web-app with Dart that is located in the web-folder.
Now i added a test-folder parallel to web with a file called test.dart:
library dartingflame_test;
import 'dart:math';
import 'package:unittest/unittest.dart';
import 'package:unittest/html_config.dart';
import 'package:mockito/mockito.dart';
import '../web/dartingflame.dart';
void main() {
useHtmlConfiguration();
test("verify robot's direction after move", verifyRobotDirectionAfterMove);
}
void verifyRobotDirectionAfterMove() {...}
Unfortunatly I now have problems to run this test file.
Run with Dartium leads to the message: Unable to locate associated html file
Run leads to: Observatory listening on http://127.0.0.1:49919
The built-in library 'dart:html' is not available on the stand-alone VM.
'package:unittest/html_config.dart': error: line 10 pos 1: library handler failed
import 'dart:html';
Using useVMConfiguration() instead of useHtmlConfiguration() doesn't help. The test execution still complaints about the import 'dart:html';-line in the library under test.
I couldn't find a solution in the "Unit Testing with Dart"-article.
UPDATE (incorporating Günter Zöchbauer's comment)
ok, i created the index.html which invokes test.dart, but now i seem to have a problem with the relative import of the lib under test import '../web/dartingflame.dart';:
Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:8081/web/dartingflame.dart
An error occurred loading file: http://localhost:8081

You need to create a web page index.html with the test script as script tag an launch it like any Dart web app. You can also run it non-interactively using content_shell. The Dart SDK contains a batch script which downloads and installs content_shell (it is left out of the SDK download to keep a reasonable download size.
There is also a package test_runner available at http://pub.dartlang.org which helps running all tests in the test directory.

Related

Azure App Service ModueNotFoundError opencensus

I am trying to use application insights inside of a flask app using:
from opencensus.ext.azure.log_exporter import AzureLogHandler
but the runtime throws the following error:
ModuleNotFoundError: No module named 'opencensus'
However it works perfectly fine locally. I don't have anything to investigate next What should I do now?
To add to what Jason shared, please review, https://pypi.org/project/opencensus/ for further troubleshooting of the module including how to export traces.

what file structure should I use for django app on google app engine

I can't get my django app to run on google app engine.
It deploys successfully but throws an error when I try connect to it in my browser.
main.py throws the following error: from wsgi import application as app ModuleNotFoundError: No module named 'wsgi'
I've looked at the question here:
ModuleNotFoundError - No module named 'main' when attempting to start service
I don't know what file structure is 'expected' for my main.py to run without errors.
This is the current structure
--static root file--main.py, app.yaml, etc.
|
voting------------
|
--voting-----------settings.py, wsgi.py, etc.
|
other stuff
my main.py currently reads (voting is the name of my project)
from voting.wsgi import application as app
I have tried voting.voting.wsgi and wsgi also. Please help
Fixed the issue by moving the static root.
Look at this for an example of how to set out the folder structure and where to place the static root: https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/appengine/standard_python37/django

The worker for a Google Cloud Platform task cannot find the logging library

I have created a simple task based on the Google Cloud Platform "update counter" push task example. All I want to do is log that it has been invoked to the Stackdriver logs.
from google.cloud import logging
logging_client = logging.Client()
log_name = 'service-log'
logger = logging_client.logger(log_name)
import webapp2
class UpdateCounterHandler(webapp2.RequestHandler):
def post(self):
amount = int(self.request.get('amount'))
logger.log_text('Service startup task done.')
app = webapp2.WSGIApplication([
('/update_counter', UpdateCounterHandler)
], debug=True)
After deploying this and invoking it, there is an error. In the logs online it says:
from google.cloud import logging
ImportError: No module named cloud
This isn't a local version, but one that I've deployed. It's hard for me to believe that I have to actually install python libraries into the production runtime. (I can't even imagine that I can.)
As the root readme states:
Many samples require extra libraries to be installed. If there is a requirements.txt, you will need to install the dependencies with pip.
Try adding the library as explained here.
When using logging from the Python standard library in App Engine, the logs also end up in Stackdriver. So you could use import logging instead of from google.cloud import logging.
When you are specifically interested in using the google.cloud.logging library, then it needs to be installed to a project folder ./lib as referred by Tudormi: here

GAE import endpoints "No module named endpoints"

I'm using pycharm to develop appengine. Now i'm trying to use endpoints and I've put
libraries:
- name: pycrypto
version: latest
- name: endpoints
version: 1.0
and then in main.py
import endpoints
But it gives me error
No module named endpoints
I can see the endpoints folder inside the GAE library. Anyone can help?
*EDIT: it is just a matter of IDE (pycharm) cant locate endpoints. The app runs fine and okay both in dev server or cloud server. There is a picture just to make it a bit clearer:
Thanks
You need to add {GAE_SDK}/lib/endpoints-1.0, not just the SDK itself. The reason you can import google is because it is directly under {GAE_SDK}. The libraries you specify in app.yaml are laid out differently due to supporting multiple versions. I believe you also need to add {GAE_SDK}/lib/protorpc-1.0/, it's just not showing because there's already an import error.
I'm using the new version of PyCharm Community and I got to config too. You need to set the Source option on each folder like endpoints in File - Setting - Project:
I've run across the following code somewhere which fixes it for me in a client script. I'm not able to say how much of it may be unnecessary. You'd need to edit the google_appengine path for your SDK installation:
sdk_path = os.path.expanduser('~/work/google-cloud-sdk/platform/google_appengine')
try:
import google
google.__path__.append("{0}/google".format(sdk_path))
except ImportError:
pass
try:
import protorpc
protorpc.__path__.append("{0}/lib/protorpc-1.0/protorpc".format(sdk_path))
except ImportError:
pass
sys.path.append("{0}/lib/endpoints-1.0".format(sdk_path))

Google App Engine Interactive Console: ImportError: No module named models

I have a django project running on my local server under the google app engine python SDK 1.9.7.
I want to run a query in the interactive console. I have a model called Flatpage
I put the seemingly appropriate handler in the app.yaml file, namely:
url: /admin/.*
script: google.appengine.ext.admin.application
login: admin
From the docs i understood that when the local development server is running the interactive console on the admin server would be running in the same environment as the a project. Yet:
the interactive console does not recognize any of my model classes and when i try to import models I get an error: ImportError: No module named models
I have tried adding the local path to the models.py file to the directory as well as various names like app.models, appname.models and projectname.models to no avail.
However. when I copy and paste the models file into the interactive console it works.
Would someone explain how to import the models file into the interactive console,
and, secondly, why the interactive console, which seemingly should already have the models defined when the server starts, needs to have the models defined again, anyway, thanks!
I'm not sure why you're adding that handler to use the console locally, but what I do is use the Development Console (available since last year), which by default runs on port 8000, from there (localhost:8000/console most likely) you can import modules from your project.
For example if you have models.py in the same directory as app.yaml, you should be able to do:
import models
entity = models.Flatpage()
with no problem.