how to debug unittest in vscode using pytest - visual-studio-code

I am trying to use vscode to debug some unit tests using pytest. One special thing is I am importing the module I need to test from another folder.
src
func.py
test
test_func.py
so inside test_func.py, I imported the function I need to test:
import func
def test_func():
...
But when I tried to debug it using vscode, it told me "module func is not found!"
Should I do some extra configuration to make it work?

Related

Pytest pluggy._manager.PluginValidationError due to the pytest-json-report plugin

I use pytest together with the pytest-json-report plugin. I have the pytest_json_modifyreport hook in the conftest.py file.
When I run the command pytest --json-report, it is OK. But when I run the simple pytest command, it yields the following: pluggy._manager.PluginValidationError: unknown hook 'pytest_json_modifyreport' in plugin.
Is it possible to get rid of that error without commenting out the hook?
According to the official documentation:
A note on hooks
If you're using a pytest_json_* hook although the plugin is not installed or
not active (not using --json-report), pytest doesn't recognize it and may fail with an internal error like this:
INTERNALERROR> pluggy.manager.PluginValidationError: unknown hook 'pytest_json_runtest_metadata' in plugin <module 'conftest' from 'conftest.py'>
You can avoid this by declaring the hook implementation optional:
import pytest
#pytest.hookimpl(optionalhook=True)
def pytest_json_runtest_metadata(item, call):
...

How to debug unit test while developping a package in Julia

Say I develop a package with a limited set of dependencies (for example, LinearAlgebra).
In the Unit testing part, I might need additional dependencies (for instance, CSV to load a file). I can configure that in the Project.toml all good.
Now from there and in VS Code, how can I debug the Unit tests? I tried running the "runtests.jl" in the debugger; however, it unsurprisingly complains that the CSV package is unavailable.
I could add the CSV package (as a temporary solution), but I would prefer that the debugger run with the configuration for the unit testing; how can I achieve that?
As requested, here is how it can be reproduced (it is not quite minimal, but instead I used a commonly used package as it give confidence the package is not the problem). We will use DataFrames and try to execute the debugger for its unit tests.
Make a local version of DataFrames for the purpose of developing a feature in it. I execute dev DataFrames in a new REPL.
Select the correct environment (in .julia/dev/DataFrames) through the VS-code user interface.
Execute the "proper" unit testing by executing test DataFrames at the pkg prompt. Everything should go smoothly.
Try to execute the tests directly (open the runtests.jl and use the "Run" button in vs-code). I see some errors of the type:
LoadError: ArgumentError: Package CategoricalArrays not found in current path:
- Run `import Pkg; Pkg.add("CategoricalArrays")` to install the CategoricalArrays package.
which is consistent with CategoricalArrays being present in the [extras] section of the Project.toml but not present in the [deps].
Finally, instead of the "Run" command, execute the "Run and Debug". I encounter similar errors here is the first one:
Test Summary: | Pass Total
merge | 19 19
PASSED: index.jl
FAILED: dataframe.jl
LoadError: ArgumentError: Package DataStructures not found in current path:
- Run `import Pkg; Pkg.add("DataStructures")` to install the DataStructures package.
So I can't debug the code after the part requiring the extras packages.
After all that I delete this package with the command free DataFrames at the pkg prompt.
I see the same behavior in my package.
I'm not certain I understand your question, but I think you might be looking for the TestEnv package. It allows you to activate a temporary environment containing the [extras] dependencies. The discourse announcement contains a good description of the use cases.
Your runtest.jl file should contain all necessary imports to run tests.
Hence you are expected to have in your runtests.jl file lines such as:
using YourPackageName
using CSV
# the lines with tests now go here.
This is a standard in Julia package layout. For an example have a look at any mature Julia such as DataFrames.jl (https://github.com/JuliaData/DataFrames.jl/blob/main/test/runtests.jl).

Visual studio code using pytest for Pyspark getting stuck at SparkSession Creation

I am trying to run a pyspark unit test in Visual studio code on my local windows machine. when i debug the test it gets stuck at line where I am creating a sparksession. It doesn't show any error/failure but status bar just shows "Running Tests" . Once it work, i can refactor my test to create sparksession as part of test fixture, but presently my test is getting stuck at sparksession creation.
Do i have to install/configure on my local machine for sparksession to work?
i tried a simple test with assert 'a' == 'b' and i can debug and test run succsfully, so i assume my pytest configurations are correct. Issue i am facing is with creating sparksession.
# test code
from pyspark.sql import SparkSession, Row, DataFrame
import pytest
def test_poc():
spark_session = SparkSession.builder.master('local[2]').getOrCreate() #this line never returns when debugging test.
spark_session.createDataFrame(data,schema) #data and schema not shown here.
Thanks
What I have done to make it work was:
Create a .env file in the root of the project
Add the following content to the created file:
SPARK_LOCAL_IP=127.0.0.1
JAVA_HOME=<java_path>/jdk/zulu#1.8.192/Contents/Home
SPARK_HOME=<spark_path>/spark-3.0.1-bin-hadoop2.7
PYTHONPATH=$SPARK_HOME/python:$SPARK_HOME/python/lib/py4j-0.10.9-src.zip:$PYTHONPATH
Go to .vscode file in the root, expand and open settings.json. Add the following like (replace <workspace_path> with your actual workspace path):
"python.envFile": "<workspace_path>/.env"
After refreshing the Testing section in Visual Studio Code, the setup should succeed.
Note: I use pyenv to setup my python version, so I had to make sure that VS Code was using the correct python version with all the expected dependencies installed.
Solution inspired by py4j.protocol.Py4JError: org.apache.spark.api.python.PythonUtils.getEncryptionEnabled does not exist in the JVM and https://github.com/microsoft/vscode-python/issues/6594

Cucumber Test Runner file-Not executing step definitions

I am building a Restassured API test framework with cucumber.(This is a new adventure for me so apologies if this seems basic)
Below is how I setup my test runner file.
package cucumber.Options;
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
#RunWith(Cucumber.class)
#CucumberOptions(features="src/test/java/features", glue={"stepDefinitions"},strict=true,stepNotifications = true)
public class TestRunner{
}
I have made sure the glue matches my step def file, ive also tried adding the full path.
However my step definitions no matter what I set as the glue are not being executed.
As soon as I run the TestRunner File as a junit test junit marks the runs as complete.
I set up a simple scenario where I am just printing a test output to console and console never shows the output.
I even tried setting my glue to a filename that doesnt even exist to see if i got an error but i get the same result as above.
And whenever I click one of the the lines in the feature file I get error "Test class not found in selected project"
Error
Has anyone experienced the same behavior with eclipse?

How to watch mocha-webpack tests results in mocha-test-explorer VS Code Extension?

I'm working on a just created Vue cli project with TypeScript and unit testing.
I Know that Vue cli tests uses mocha-webpack to runt tests. And when I Type mocha-webpack on terminal, the tests runs ok.
But I would like to use an mocha-test-explorer, a VS Code extension for mocha testing. For a better programming experience.
I Installed the extension and configured like this:
"mochaExplorer.files": "tests/**/*.ts",
"mochaExplorer.require": "ts-node/register"
But I'm still not able to see the tests. test-explorer shows the fallowing output:
(function (exports, require, module, __filename, __dirname) { import { expect } from 'chai'
^^^^^^
SyntaxError: Unexpected token import
I know that is because mocha is not loading files through webpack.
What should I do to see mocha-webpack tests in the mocha-test-explorer extension (or any other gui extension) for VSCode?