Pytest session scoped fixture is calculated several times - pytest

I have following structure of tests:
tests
|__element
| |__test_create.py
| |__test_delete.py
| |__test_get.py
| |__test_recover.py
| |__test_update.py
|__conftest.py
I have fixture in conftest.py which must take the same value throughout the session:
#pytest.fixture(scope="session")
async def workspace_locales_set() -> list[str]:
random_locales_subset = random.sample(list(LOCALES_ALLOWED.keys()), random.randint(3, 7))
print("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", random_locales_subset)
return random_locales_subset
But all the tests, in test_update fall down because there comes a different locales value.
By the method of many prints, I found out that at the moment of switching from test_recover to test_update`, the fixture is calculated a second time
Here is tests report:
collecting ... collected 893 items
test_create.py::TestElementCreateFieldName::test_name_value_locales_allowed
test_create.py::TestElementCreateFieldName::test_name_value_locales_not_allowed
...
test_delete.py::TestElementsDeleteExisting::test_element_existing_after_deletion[True]
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA ['sv-FI', 'gsw-CH', 'zh-Hant-TW']
PASSED [ 0%] PASSED [ 1%]
...
test_create.py::TestElementCreateFieldAttributesFileInput::test_attrs_file_allowed_file_types_is_not_enum_item PASSED [ 5%]
test_delete.py::TestElementsSoftDeleteFieldIsDeleted::test_element_is_deleted PASSED [ 18%]PASSED [ 18%]
test_get.py::TestElementsGetFieldName::test_name_value PASSED [ 19%]PASSED [ 19%]PASSED [ 20%]PASSED [ 20%]
test_recover.py::TestElementsRecoverFieldIsDeleted::test_element_is_deleted
test_recover.py::TestElementsRecoverAccessRights::test_recovering_by_user_not_in_parent_workspace
test_update.py::TestElementsUpdateName::test_update_name_value PASSED [ 24%]PASSED [ 24%]PASSED [ 24%]PASSED [ 24%]PASSED [ 24%]PASSED [ 24%]PASSED [ 24%]
ERROR [ 24%]
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA ['es-CL', 'kn', 'xog', 'es-PY', 'hi-IN', 'en-AS', 'en-TT']
As you can see, here is something weird: first the creation tests are run, then one deletion test, then the fixture is computed the first time, then the rest of the run tests go, and between the recovery tests and the deletion test the fixture is computed the second time
I use pytest-asyncio with asyncio_mode = auto. Could it have any effect?
But anyway, the session fixture should be calculated only once. What am I doing wrong?

Okay, that was really stupid. My PyCharm automatically added from conftest import workspace_locales_set in test_update file, so the fixture was called twice.
To fix it just delete all imports from conftest

Related

Flow Enums correctly parsed but not transformed

In my React app, I'm trying to migrate from my "old school" JS enums to Flow Enums:
https://flow.org/en/docs/enums/
(I think) I've done everything listed here:
https://flow.org/en/docs/enums/enabling-enums/
eslint and flow check are both happy (zero error) and the enums work as expected when I type code.
But when I start my app, they are not transformed and I get this:
ERROR in ./src/types.js
Module build failed (from ../../node_modules/babel-loader/lib/index.js):
SyntaxError: C:\foo\src\types.js: Unexpected token, expected "{" (16:7)
14 | |};
15 |
> 16 | export enum FooEnum {
| ^
17 | On,
18 | Off,
19 | Default
at instantiate (C:\foo\node_modules\#babel\parser\lib\index.js:72:32)
at constructor (C:\foo\node_modules\#babel\parser\lib\index.js:366:12)
at FlowParserMixin.raise (C:\foo\node_modules\#babel\parser\lib\index.js:3453:19)
at FlowParserMixin.unexpected (C:\foo\node_modules\#babel\parser\lib\index.js:3491:16)
at FlowParserMixin.parseExport (C:\foo\node_modules\#babel\parser\lib\index.js:16044:16)
at FlowParserMixin.parseExport (C:\foo\node_modules\#babel\parser\lib\index.js:6170:24)
at FlowParserMixin.parseStatementContent (C:\foo\node_modules\#babel\parser\lib\index.js:14893:27)
at FlowParserMixin.parseStatement (C:\foo\node_modules\#babel\parser\lib\index.js:14777:17)
at FlowParserMixin.parseStatement (C:\foo\node_modules\#babel\parser\lib\index.js:5951:24)
at FlowParserMixin.parseBlockOrModuleBlockBody (C:\foo\node_modules\#babel\parser\lib\index.js:15420:25)
Package-wise, all of them are in their latest version and I've installed:
babel-plugin-transform-flow-enums
eslint-plugin-ft-flow
flow-enums-runtime
My Babel config is:
"babel": {
"plugins": [
"#babel/plugin-proposal-class-properties",
[
"#babel/plugin-syntax-flow",
{
"enums": true
}
],
"babel-plugin-transform-flow-enums"
],
"presets": [
"#babel/preset-env",
"#babel/preset-flow",
"#babel/preset-react"
]
},
Also, calling Babel from a command line correctly transforms the enum. I'm using this command:
npx babel src/types.js
What could I have missed?
So, after struggling for hours, I eventually found out that
react-app-rewired was messing up with my Babel plugins.
I ended up installing customize-cra, which allowed me to explicitely use my Babel config:
const {useBabelRc, override} = require('customize-cra');
module.exports = override(
useBabelRc()
);

How to customize system calls in gVisor?

I am a student and try to customize the system calls in gVisor. I have successfully compiled the gVisor on go-branch. And I have got the right message when I change the pkg/sentry/kernel/syslog.go file. Here is the result that can show I have successfully compiled the runsc (the runtime of gVisor).
sudo docker run --runtime=runsc -it ubuntu dmesg
[ 0.000000] asdf Starting gVisor...
[ 0.360765] 6666...
[ 0.529799] 5555...
[ 0.959593] iiiiii...
[ 1.343602] 7777...
[ 1.347068] 4444...
[ 1.424063] 00000...
[ 1.470641] 22222...
[ 1.858755] 99999...
[ 2.213219] 8888...
[ 2.679995] cccccc ..
[ 2.943468] asdf asdf Setting up VFS2...
[ 3.429006] Ready!
And I have noticed the package gvisor/pkg/sentry/syscalls/linux which contains all the syscalls and they are registered in file gvisor/pkg/sentry/syscalls/linux/linux64.go. However, I failed to customize the syscalls in gVisor.
Thanks very much.

How to reuse deps for different tests in Bazel?

I am using Bazel to compile scala.
Right now, my scala_test looks like
scala_test {
name = "sample",
srcs = [
"a.scala",
"b.scala",
"c.scala",
"d.scala",
],
deps = [
"//src//main/scala/.../dep1",
"//src//main/scala/.../dep2",
"//src//main/scala/.../dep3",
"//src//main/scala/.../dep4",
]
}
In this case, Bazel does not support parallelization on these srcs as they are grouped as one scala_test. To enable automatic parallel testing, I would like to separate srcs into different scala_test like
scala_test {
name = "sample1",
srcs = [
"a.scala",
],
deps = [
"//src//main/scala/.../dep1",
"//src//main/scala/.../dep2",
"//src//main/scala/.../dep3",
"//src//main/scala/.../dep4",
]
}
scala_test {
name = "sample2",
srcs = [
"b.scala",
],
deps = [
"//src//main/scala/.../dep1",
"//src//main/scala/.../dep2",
"//src//main/scala/.../dep3",
"//src//main/scala/.../dep4",
]
}
...
The problem is I guess bazel tries to compile the deps for every scala_test. Is there any way to group dependencies and reuse them in different scala_test blocks such as scala_library?
Sorry, I think Bazel caches the dependencies so I don't have to worry about compiling them again when running all tests.

Report tests with excluded tags as skipped [duplicate]

I have some tests I marked with an appropriate marker. If I run pytest, by default they run, but I would like to skip them by default. The only option I know is to explicitly say "not marker" at pytest invocation, but I would like them not to run by default unless the marker is explicitly asked at command line.
A slight modification of the example in Control skipping of tests according to command line option:
# conftest.py
import pytest
def pytest_collection_modifyitems(config, items):
keywordexpr = config.option.keyword
markexpr = config.option.markexpr
if keywordexpr or markexpr:
return # let pytest handle this
skip_mymarker = pytest.mark.skip(reason='mymarker not selected')
for item in items:
if 'mymarker' in item.keywords:
item.add_marker(skip_mymarker)
Example tests:
import pytest
def test_not_marked():
pass
#pytest.mark.mymarker
def test_marked():
pass
Running the tests with the marker:
$ pytest -v -k mymarker
...
collected 2 items / 1 deselected / 1 selected
test_spam.py::test_marked PASSED
...
Or:
$ pytest -v -m mymarker
...
collected 2 items / 1 deselected / 1 selected
test_spam.py::test_marked PASSED
...
Without the marker:
$ pytest -v
...
collected 2 items
test_spam.py::test_not_marked PASSED
test_spam.py::test_marked SKIPPED
...
Instead of explicitly say "not marker" at pytest invocation, you can add following to pytest.ini
[pytest]
addopts = -m "not marker"

Karma preprocessor not running

My karma.conf.js includes:
plugins: [
'karma-jasmine',
'karma-phantomjs-launcher',
'karma-ng-html2js-preprocessor'
],
preprocessors: {
'../../mypath/*.html': ['ng-html2js']
},
ngHtml2JsPreprocessor: {
moduleName: 'templates'
},
(I've tried without specifying any plugins, too.)
My devDependencies include:
"karma-ng-html2js-preprocessor": "^0.2.0"`
My tests include:
beforeEach(module('templates'));
These give the error:
Module 'templates' is not available!
Running karma with --log-level debug, I do not see any [preprocessor.html2js] entries. (I do get Loading plugin karma-ng-html2js-preprocessor.)
What am I doing wrong?
The issues were that the templates must be listed under files as well, and that the glob pattern in preprocessors must match. This is implied by the documentation.
files: [
'../../Scripts/angular-app/directives/*.html',
// .js files
],
preprocessors: {
'../../Scripts/angular-app/**/*.html': ['ng-html2js']
},
Note that **/*.html does not match parent directories of the basePath.
karma start --log-level debug will display DEBUG [preprocessor.html2js] entries when everything is correct.
I was also able to remove the plugins section.
To get the correct cache ID, I used:
ngHtml2JsPreprocessor: {
// Load this module in your tests of directives that have a templateUrl.
moduleName: 'templates',
cacheIdFromPath: function (filepath) {
return filepath.substring(filepath.indexOf('/Scripts/angular-app/'));
}
},
If a template references a custom filter, the filter must be loaded in files and the filter's module must be loaded in your directive tests.