How do you run OpenERP yaml unit tests - unit-testing

I'm trying to run unit tests on my openERP module, but no matter what I write it doesnt show if the test passes or fails! Anyone know how to output the results of a test? (Using Windows OpenERP version 6.1)
My YAML test is:
-
I test the tests
-
!python {model: mymodelname}: |
assert False, "Testing False!"
assert True, "Testing True!"
The output when I reload the module with
openerp-server.exe --update mymodule --log-level=test -dtestdb
shows that the test ran but has no errors?!
... TEST testdb openerp.tools.yaml_import: I test the tests
What am I doing wrong?
Edit: ---------------------------------------------------------------------
Ok so after much fiddling with the !python, I tried out another test:
-
I test that the state
-
!assert {model: mymodel, id: mymodel_id}:
- state == 'badstate'
Which gave the expected failure:
WARNING demo_61 openerp.tools.yaml_import: Assertion "NONAME" FAILED
test: state == 'badstate'
values: ! active == badstate
So I'm guessing it is something wrong with my syntax which may work as expected in version 7.
Thanks for everyone's answers and help!

This is what I've tried. It seems to work for me:
!python {model: sale.order}: |
assert True, "Testing True!"
assert False, "Testing False!"
(Maybe you forgot the "|" character)
And then :
bin/start_openerp --init=your_module_to_test -d your_testing_database --test-file=/absolute/path/to/your/testing_file.yml
You might want to create your testing database before :
createdb mytestdb --encoding=unicode
Hope it helps you
UPDATE: Here are my logs ( I called my test file sale_order_line_test.yml)
ERROR mytestdb openerp.tools.yaml_import: AssertionError in Python code : Testing False!
mytestdb openerp.modules.loading: At least one test failed when loading the modules.
loading test file /path/to/module/test/sale_order_line_test.yml
AssertionError in Python code : Testing False!

Looking at the docs (e.g. here and here), I can't see anything obviously wrong with your code.
However, I'm not familiar with --log-level=test. Maybe try running it with the -v, --debug or --log-level=debug flags instead of --log-level=test? You may also need to try the uppercase variants for the --log-level argument, i.e. --log-level=DEBUG.
test certainly isn't one of the standard Python logging module's logging levels, and while I can't exclude the possibility of them adding a custom log level, I don't think that's the case.
It might also be worthwhile trying to remove the line obj = self.browse(cr, uid, ref("HP001")), just in case..

Try to type following path on your terminal when you start your server.
./openerp-server --addons-path=<..Path>...--test-enable
:Enable YAML and unit tests.
./openerp-server --addons-path=<..Path>...--test-commit
:Commit database changes performed by YAML or XML tests.

Try this in your terminal it will work.
./openerp-server --addons-path=<..Path> --log-level=test --test-enable
Hope This will help you.

Related

Go not running some tests

I'm new to Golang and I'm trying to run some tests from a golang app.
Tests are running at a docker container with a Golang 1.12.
My problem is that some tests appear to be running correctly, but others do not.
Example:
I have a function that I wanted to fail on purpose.
func TestLol(t *testing.T) {
assert.EqualValues(t, 1, 2)
t.Fail()
}
When I execute the docker container with a "docker run ... go test -v ./..." it should run all tests and about this function on specific it should fail, but what happens that it doesn't fail. Golang just log a "ok" beside the test.
Then I tried to run only the folder with test file that should fail.
Log:
ok github.com/eventials/csw-notifications/services 0.016s
2021/09/25 21:08:44 Command finished successfully.
Tests exited with status code: 0
Stopping csw-notifications_db_1 ... done
Stopping csw-notifications_broker_1 ... done
Going to remove csw-notifications_app_run_ed70597b5c20, csw-notifications_db_1, csw-notifications_broker_1
Removing csw-notifications_app_run_ed70597b5c20 ... done
Removing csw-notifications_db_1 ... done
Removing csw-notifications_broker_1 ... done
My question is why golang dosn't output any log with a FAIL message for this file in specific?
I think it's somewhat related to this question, but as it didn't received any answear I'm reposting it.
Why the tests are not running ? ( Golang ) - goapp test - bug?
EDIT: I'm editting to this question be more clear.
You can try add -timeout
If your test files contain FuncTest with samenames rename.
You can try change output to go test -v -json ./...
-timeout d
If a test binary runs longer than duration d, panic.
If d is 0, the timeout is disabled.
The default is 10 minutes (10m).
main.go
func Start(s ...string) {
fmt.Println(s)
}
main_test.go
func TestStart(t *testing.T) {
Start("rocket")
if 0 == 1 {
t.Log("Houston, we have a problem")
t.Fail()
}
}
func ExampleStart() {
fmt.Println("Ground Control to Major Tom")
// Output:
// Ground Control to Major Tom
}
Change if condition to 0 == 0 to see Fail with somelogs.
I found out that a import was causing this issue.
Every test file importing porthos-go or porthos-go/mock isn't running the test files. Removing those imports fixed my problem.
lib: https://github.com/porthos-rpc/porthos-go
I still don't know why, but when I do I'll update this answear.

Why is Jest not outputting my assertions to the terminal when it runs?

When I run my test I see. None of the assertions I've written
What I'm expecting is something like this:
I can see the individual assertions with a tick next to them.
This is the expected output of jest. You just get a single success message per suite. If tests fail, then you get more detailed info about the failed tests.
https://github.com/facebook/jest/issues/148
This can be changed by running jest --verbose

ODOO [V8] Unit Tests

I'm actually trying to run the unittests I've created thanks to Odoo's documentation.
I've built my module like this :
module_test
- __init__.py
__openerp.py__
...
- tests
__init__.py
test_1.py
Inside 'module_test/tests/init.py', I do have "import test_1"
Inside, 'module_test/tests/test_1.py", I do have : "import tests + a test scenario I've written.
Then I launch the command line to run server, and I add :
'-u module_test --log-level=test --test-enable' to update the module and activate the tests run
The shell returns : "All post-tested in 0.00s, 0 queries".
So in fact, no tests are run.
I then added a syntax error, so the file can't be compiled by the server, but shell returned the same sentence. It looks like the file is ignored, and the server is not even trying to compile my file... I do not understand why ?
I've checked some Odoo source module, the 'sale' one for example.
I've tried to run sale tests, shell returned the same value than before.
I added syntax error inside sale tests, shell returned the same value again, and again.
Does anyone have an idea about this unexpected behavior ?
You should try using post_install decorator for test class:
Example:
from openerp.tests import common
#common.post_install(True)
class TestPost(common.TransactionCase):
def test_post_method(self):
response = self.env['my_module.my_model'].create_post('hello')
self.assertEqual(response['success'], True)
To make the tests perform faster without updating your module, you should be able to run tests without
-u module_test
if you use
--load=module_test
I have to admit that odoo testing documentation is really bad. It took me a week to figure out how to make unit testing work in odoo.

Go: how to run tests for multiple packages?

I have multiple packages under a subdirectory under src/,
running the tests for each package with go test is working fine.
When trying to run all tests with go test ./... the tests are running but it fails..
the tests are running against local database servers, each test file has global variables with db pointers.
I tried to run the tests with -parallel 1 to prevent contention in the db, but the tests still fail.
what can be the issue here?
EDIT: some tests are failing on missing DB entries, I completely clear the DB before and after each test. the only reason I can think of why this is happening is because of some contention between tests.
EDIT 2:
each one of my test files has 2 global variables (using mgo):
var session *mgo.Session
var db *mgo.Database
also it has the following setup and teardown functions:
func setUp() {
s, err := cfg.GetDBSession()
if err != nil {
panic(err)
}
session = s
db = cfg.GetDB(session)
db.DropDatabase()
}
func tearDown() {
db.DropDatabase()
session.Close()
}
each tests startup with setUp() and defer tearDown()
also cfg is:
package cfg
import (
"labix.org/v2/mgo"
)
func GetDBSession() (*mgo.Session, error) {
session, err := mgo.Dial("localhost")
return session, err
}
func GetDB(session *mgo.Session) *mgo.Database {
return session.DB("test_db")
}
EDIT 3:
I changed cfg to use a random database, the tests passed.
it seems that the tests from multiple packages are running somewhat in parallel.
is it possible to force go test to run everything sequentially across packages ?
Update: As pointed out by #Gal Ben-Haim, adding the (undocumented) go test -p 1 flag builds and tests all packages in serial. As put by the testflag usage message in the Go source code:
-p=n: build and test up to n packages in parallel
Old answer:
When running go test ./..., the tests of the different packages are in fact run in parallel, even if you set parallel=1 (only tests within a specific package are guaranteed to be run one at a time). If it is important that the packages be tested in sequence, like when there is database setup/teardown involved, it seems like the only way right now is to use the shell to emulate the behavior of go test ./..., and forcing the packages to be tested one by one.
Something like this, for example, works in Bash:
find . -name '*.go' -printf '%h\n' | sort -u | xargs -n1 -P1 go test
The command first lists all the subdirectories containing *.go files. Then it uses sort -u to list each subdirectory only once (removing duplicates). Finally all the subdirectories containing go files get fed to go test via xargs. The -P1 indicates that at most one command is to be run at a time.
Unfortunately, this is a lot uglier than just running go test ./..., but it might be acceptable if it is put into a shell script or aliased into a function that's more memorable:
function gotest(){ find $1 -name '*.go' -printf '%h\n' | sort -u | xargs -n1 -P1 go test; }
Now all tests can be run in the current directory by calling:
gotest .
apparently running go test -p 1 runs everything sequentially (including build), I haven't see this argument in go help test or go help testflag
I am assuming that because the packages individually pass that in this situation you are also dropping the DB before that test as well.
Therefore it sounds like the state of the DB for each package test is expected to be empty.
So between each set of the package tests the DB must be emptied. There are two ways around this, not knowing your entire situation I will briefly explain both options:
Option 1. Test Setup
Add an init() function to the start of each package _test file which you then put processing to remove the DB. This will be run before the init() method of the actual package:
func init() {
fmt.Println("INIT TEST")
// My test state initialization
// Remove database contents
}
Assuming that the package also had a similar print line you would see in the output (note the stdout output is only displayed when the a test fails or you supply the -v option)
INIT TEST
INIT PACKAGE
Option 2. Mock the database
Create a mock for the database (unless that is specifically what you are testing). The mock db can always act like the DB is blank for the starting state of each test.
Please try out the following github repository.
https://github.com/appleboy/golang-testing
Copy coverage.sh to /usr/local/bin/coverage and change permission.
$ curl -fsSL https://raw.githubusercontent.com/appleboy/golang-testing/master/coverage.sh /usr/local/bin/coverage
$ chmod +x /usr/local/bin/coverage

Codeception - Acceptance tests work but Functional test don't

I am running the latest version of Codeception on a WAMP platform - My acceptance is very basic however works fine (see below):
$I = new WebGuy($scenario);
$I->wantTo('Log in to the website');
$I->amOnPage('/auth/login');
$I->fillField('identity','admin#admin.com');
$I->fillField('password','password');
$I->click('Login');
In a nutshell - it checks the page is 'auth/login' fills out 2 form fields and clicks the login button. This works without any problems.
Here is my identical functional test:
$I = new TestGuy($scenario);
$I->wantTo('perform actions and see result');
$I->amOnPage('/auth/login');
$I->fillField('identity','admin#admin.com');
$I->fillField('password','password');
$I->click('Login');
When I run this from the command line I get the following error (not the full error but enough to understand the problem):
1) Couldn't <-[35;1mperform actions and see result<-
[0m in <-[37;1LoginCept.php<-[0m <-41;37mRuntimeException:
Call to undefined method TestGuy::amOnPage<-[0m.......
My Acceptance suite has 'PhpBrowser' & 'WebHelper' modules enabled, the Functional suite has 'FileSystem' & 'TestHelper' enabled (within the acceptance.suite.yml & functional.suite.yml files)
Obviously the amOnPage() function is the problem - however I am led to believe amOnPage() should work in acceptance and functional test? Or I am wrong - also - can someone explain what the numbers mean e.g '<-[35;1m' that appear
UPDATE: I tried adding the 'WebHelper' module to the functional.suite.yml but I do not see the amOnPage() being auto-generated in the TestGuy.php file - any ideas?
My config files are below:
WebGuy
class_name: WebGuy
modules:
enabled:
- PhpBrowser
- WebHelper
config:
PhpBrowser:
url: 'http://v3.localhost/'
TestGuy
class_name: TestGuy
modules:
enabled: [Filesystem, TestHelper, WebHelper]
Well, this is so, because of TestGuy don't have those methods. All of those methods are in the PhpBrowser, Selenium2 modules or other that inherits from Codeception Mink implementation. So you need to add PhpBrowser in your functional suite in modules section, and then run codecept build command.
Also note that it is better to use Selenium2 module for acceptance test and PhpBrowser for functional tests. The main idea is that acceptance(Selenium2) tests must cover those part of your application, that can not be covered by functional (PhpBrowser) tests, for example some js-interactions.
About '<-[35;1m' start script codecept run --no-colors to remove '<-[35;1m' from console output