I am trying to write a unittest for my class. First of all, i install the package unittest and create a spec folder to save all my unittest files. Then i create a test dart file with following contents:
library i18n_test;
import 'package:unittest/unittest.dart';
void main() {
}
When i run the file, i got the following error
Unable to open file: D:\Dart\i18n\bin\spec\packages\unittest\unittest.dart'file:///D:/Dart/i18n/bin/spec/i18n_test.dart': error: line 3 pos 1: library handler failed
import 'package:unittest/unittest.dart';
As i mentioned, i saved my unittest file in the spec folder, i think that because the compiler could not find the package unittest.
My Folder structure looks like:
What do i wrong?
The error is caused by a missing packages symlink in your spec folder.
see https://code.google.com/p/dart/issues/detail?id=16947
Anyway
unit tests should be put into the test folder (i18n/test).
This way your test code won't be included in the build output.
It's best to have most of your code in the lib folder to make it easy to import into your tests using package imports like:
import 'package:i18n/my_dart_file.dart';
you should still be able to import from bin or web using relative imports like
import '../bin/my_dart_file.dart';
but that form is not very refactoring-friendly.
Related
I have some unit tests and some classes that are related only to tests, like factories for my models, some extensions on mockito and others...
The issue is when I try to import those files from my tests I can just import them with relative paths, lets say I have this utilities:
test/src/test_utils/factories.dart
test/src/test_utils/mocks.dart
test/src/test_utils/mockito_extensions.dart
How I can not do it
But When I try to import them from my tests It can not find them with
import 'package:myapp/test_utils/mocks.dart';
or:
import 'package:myapp/src/test_utils/mocks.dart';
The only one that works
The only way to import them is by relative paths like:
import '../../test_utils/mocks.dart';
The question
I would love to understand what is happening here, why my tests cant find the test utils class on the imports and whats the best way to do this.
In the pub package layout the package: URI format is used for files under lib/. These are the "public libraries" that can be used from outside the package, and so they are the ones associated with the package name. Files under test/ are private to the package and don't need to be referenced by package name - they only can be referenced from neighboring files and directories so a relative import is sufficient. The package: URI format is designed primarily because it allows imports to other packages, to which there is no stable relative path.
See https://dart.dev/tools/pub/package-layout#public-libraries
Using relative imports is the recommended approach.
Jest in my application is not able to find imported file and ends with this error:
FAIL pages_test_files/index.spec.js
● Test suite failed to run
Cannot find module '../styles/sassTokenExports/colors.json' from 'index.js'
However, Jest was able to find:
'../pages/index.js'
You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['js', 'json', 'jsx', 'ts', 'tsx', 'node'].
See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string
I am using Jest in nextJS for Unit testing.
I had this problem. I had provided the absolute import path for the module I was trying to import and was receiving the same error. Turned out the base url for imports had been set in the config.
The base url was set to ./src but I was including ./src in my import like
import myModule from './src/myFolder/myModule'
which was the equivalent of
'./src/src/myFolder/myModule'.
I removed the /src and the error went away.
Some info here https://levelup.gitconnected.com/understand-and-configure-absolute-import-paths-in-javascript-5cde3be2630d
I am testing Google Cloud and first I wanted to develop something on my PC before I use in on google cloud.
I am using APACHE and configured it in that way, that when I am going to the page localhost/wsgi_app I see my page which physically is stored in folder /svc/http/webapp2. File wsgi_app.py which contains my app is stored in subfolder webapp2. All works fine. I provide this information just in case it might play any role.
The issue I have is with import from library.
I did it on Django and now try to move it to webapp2.
The first lines of my program look like this:
import webapp2
import MySQLdb
import json
I have file called test.py which contains some classess and funtions.
it is in the same folder as wsgi_app.py.
I want to include it, however this seems not to work:
import webapp2
import MySQLdb
import json
from test import *
my test.py contains definition of the class 'Quote', but when I call the page I see error
NameError: global name 'Quote' is not defined
When I put the inside of the file test.py in the file wsgi_app.py all works fine.
My goal is to separate the code into several files.
Any idea why :
from test import *
does not work ?
It worked on any other program I wrote, so why not here?
issue solved.
issue is not related to weapp2 but to WSGI and the path where python search for files.
Most simple solution is to add something like this ;
execfile("/srv/http/test.py")
it will import the file.
other than this here is quite good article
http://blog.dscpl.com.au/2014/09/python-module-search-path-and-modwsgi.html
I am making a import package and I keep running into a problem when I try and import and use the package. Primarily when I try to run it.
import mypackage
once I import it I can use it I dont get any errors upon importing it, however when I go to use it I get an error that says
TypeError: 'module' object is not callable
In a folder that I have named myfolder which is located in my current working directory i have a empty init.py file as well as two other .py files which contain the functions i have one called test.py and another called testing.py.... my problem isnt importing them but when I go to use them I get the TypeError.
the folders structure is as follows,
current working directory
-Mypackage
-__init__.py
-test.py
-testing.py
I import it like this,
from Mypackage import test
I use it like this,
test(stuff)
You have to learn how to use modules in python. When you import mypackage into your script it imports a module which has two scripts in it. Let's say they are first.py and second.py . Now if you use something like
from mypackage import first
And then you use a function named func like this
first.func()
Then it should work.
In short either import all the functions from your module or use dot notation to reference these functions. More on module can be read here
I have a Python module I'm working on to add some unit tests.
The project layout looks something like this:
myproj\
__init__.py
myproj.py
test\
test_myproj.py
In test_myproj.py I'm trying to import the parent module as follows:
from .. import myproj
When I try and run it though, I get this:
Traceback (most recent call last):
File "C:\Projects\myproj\test\test_myproj.py", line 6, in <module>
from .. import myproj
SystemError: Parent module '' not loaded, cannot perform relative import
Various sources on the internet suggest this should work so what am I missing?
You have to make your test directory a package - add __init__.py file to the test directory. After this you need your test_myproj.py file to have a __name__ of myproj.test.test_myproj when it is executed, so that it could compute the relative import path to myproj. Which will lead to from .. import myproj being interpreted as from myproj import myproj. But when you run your test file directly the __name__ is set to __main__, so it can't calculate relative import. To fix this, the test file should be imported instead of being run directly. Create a separate file (e.g. testrunner.py), which you will run to perform tests, and in that file import your test module(s) (e.g. test_myproj.py), obviously without relative import syntax or the problem will repeat. The import will lead to your test file being executed with the __name__ value, that will allow to calculate relative import. E.g.:
Add testrunner.py as shown:
testrunner.py
myproj\
__init__.py
myproj.py
test\
__init__.py
test_myproj.py
testrunner.py content:
from myproj.test import test_myproj.py
run your test using:
python C:\Projects\testrunner.py
or
cd C:\Projects\
python testrunner.py
This way it's probably best to add another directory that will keep both testrunner.py and your myproj package.
See more thorough explanation on relative import in the answer here.
There's a note in official docs for this matter:
Note that relative imports are based on the name of the current module. Since the name of the main module is always "__main__", modules intended for use as the main module of a Python application must always use absolute imports.