VSCode does not detect any of my pytest tests - visual-studio-code

I am trying to get my unit tests to show up in VSCode's test explorer, but it does not seem to be working. The test discovery does not fail with any errors in the output, but also does not show any of my tests.
This is the repo I am working in, you can see the file structure there.
This may or may not have anything to do with me using poetry to manage my virtual environment, though I'm sure the python interpreter is set correctly. I can get my tests to run perfectly well manually by running pytest or poetry run pytest in my base directory. I do have an empty __init__.py in my tests directory. Also of note is that I'm running in Ubuntu on WSL 2.
I set this up environment with the following process:
Install WSL2 on Windows
Install VSCode on Windows
Install the 'Remote - WSL' VSCode extension
Open a new WSL Window in VSCode
Install Anaconda in Ubuntu using this guide
Install the VSCode Python extension
Set the VSCode python interpreter to the anaconda install, and open a new terminal
pip install poetry
Go to my project root folder
poetry install
Set the VSCode python interpreter to the poetry environment, and open a new terminal
Done, I can run pytest via the command line
Here's my workspace setting.json:
{
"restructuredtext.confPath": "${workspaceFolder}/docs/source",
"python.testing.pytestEnabled": true,
}
Here's what the output shows when I try to refresh tests:
~/.cache/pypoetry/virtualenvs/monaco-bfS1OgpY-py3.9/bin/python ~/.vscode-server/extensions/ms-python.python-2021.11.1422169775/pythonFiles/testing_tools/run_adapter.py discover pytest -- --rootdir ~/coding/monaco -s --cache-clear
cwd: ~/coding/monaco
If I run the above command manually in the terminal, it prints out a large dict which looks like it describes all my tests. (Below, truncated but it goes on like this for a while).
[{"rootid": ".", "root": "/mnt/c/Users/Scott/Documents/Documents/Coding/monaco", "parents": [{"id": "./tests", "kind": "folder", "name": "tests", "parentid": ".", "relpath": "./tests"}, {"id": "./tests/test_MCCase.py", "kind": "file", "name": "test_MCCase.py", "parentid": "./tests", "relpath": "./tests/test_MCCase.py"}, {"id": "./tests/test_MCSim.py", "kind": "file", "name": "test_MCSim.py", "parentid": "./tests", "relpath": "./tests/test_MCSim.py"}, {"id": "./tests/test_MCSim_io.py", "kind": "file", "name": "test_MCSim_io.py", "parentid": "./tests", "relpath": "./tests/test_MCSim_io.py"}, {"id": "./tests/test_MCVal.py", "kind": "file", "name": "test_MCVal.py", "parentid": "./tests", "relpath": "./tests/test_MCVal.py"},

prima facie it seems that either
the directory saved in settings.json is different from your actual working directory or
Something wrong with the installation of pytest which the VS code can access,
while the one from the terminal seems to be working as you said.
Anyway, to help you we need more info.
your folder structure, files list, etc
how you installed python, pytest and linked it with VS code
output of
> ~/.cache/pypoetry/virtualenvs/myproject-bfS1OgpY-py3.9/bin/python ~/.vscode-server/extensions/ms-python.python-2021.11.1422169775/pythonFiles/testing_tools/run_adapter.py discover pytest -- --rootdir ~/coding/myproject/tests -s --cache-clear .
cwd: ~/coding/myproject/tests
even if it is gibberish
any other info you think could help and have forgotten to include here

I figured this out. It turns out that the only reason pytest was working at all, was because I had installed the package locally and the test imports were keying off the local files via that package installed in editable mode (this happens with poetry install or pip install -e).
I started from a clean python environment, and got pytest working by using the "conftest" answer to this stackoverflow question. My tests show up now!
Edit 2022-01-24: I am running into this issue again, and unfortunately a clean environment is not working any more.

Related

Install additional packages in vscode dev container

I am using vscode as an editor. Using vscode ctrl+shift+p to open up the command pallet and used "Dev Containers: Create Dev container" to create a development container, And it created only one file .devcontainer/devcontainer.json .
{
"name": "Debian",
"image": "mcr.microsoft.com/devcontainers/base:bullseye",
"features": {
"ghcr.io/devcontainers/features/docker-from-docker:1": {},
"ghcr.io/devcontainers/features/github-cli:1": {},
"ghcr.io/devcontainers/features/go:1": {},
"ghcr.io/devcontainers/features/python:1": {},
"ghcr.io/guiyomh/features/golangci-lint:0": {}
}
}
But I would like to install few other packages like jinja2-cli after installation of python/pip3 package mentioned in the features or may be install other OS related packages.
Where should packages should be specified ?
Check that this line is included in your devcontainer.json
"postCreateCommand": "pip3 install --user -r requirements.txt",
Then create a requirements.txt file on the same level as you .devcontainer folder (not inside of it, outside).
If you add a package name to the requirements.txt file, it'll be added to the devcontainer at build time.
You can see an example in this pull request I recently completed which needed a new package and I had to add it to my devcontainer in the last commit.
Side note: You'll see that I also updated my poetry.lock file in a previous commit (using the poetry add package-name in the command line) because that project uses poetry for package management too. You don't need to do that if you aren't using poetry.

Unable to launch debugging in docker-based code-server

I first installed a code-server docker container on my Mac.
Then I installed g++ and gdb manually by running docker exec -it code-server /bin/bash
I wrote the following context in launch.json and task.json in/${workspaceFolder}/.vscode .[] []
I found out that it could be built normally, but debugging couldn't be launched.
By using "Terminal">"Run Build Task", the C++ source file could be compiled normally with creating an executable file. But when using "Run">"Start Debugging", it does nothing, including compile the source file.
1.I tried to delete the line "preLaunchTask": "build" and see if this went wrong. It didn't work.
2.I tried to use gdb and g++ manually, both of which normally.

How do you get parcel to open in a specific browser of my choice?

My parcel bundler opens up in default MS edge but i want it open local host in firefox.
Both work on Win10 and Linux Ubuntu 20.04. If you have parcel installed globally
In your root project folder enter the command(terminal)
parcel --open firefox index.html
OR
If you have parcel installed locally than change your package.json
"scripts": {
"dev": "parcel --open firefox index.html",
"prod": "parcel build index.html"
},
And hit in terminal npm run dev
You are done ! :-) Good Luck and Best regards;-)

How to install vscode extension that is being developped locally from an unpacked directory

I am developping a VSCode extension that I want to try it out in the current VSCode instance, not in a new instance.
Is there a way to install the vscode extension that is developping locally from an unpacked directory, not from a .vsix file.
In Atom editor, I can use atom link to do that. Is there something similar in VSCode?
You can install the extension locally with code --install-extension and install a locally packaged extension. I am doing this with esbuild and yarn:
package.json
"scripts": {
"vscode:prepublish": "yarn run build:base --minify",
"build:base": "esbuild ./src/extension.ts --bundle --outfile=out/extension.js --external:vscode --format=cjs --platform=node",
"build": "yarn build:base --sourcemap",
"package": "mkdirp dist && vsce package -o dist/noteberry.vsix --yarn",
"deploy:local": "yarn package && code --install-extension dist/ext.vsix --force"
...
},
mkdirp is an npm module to create folders.
Original Post
One easy solution without packaging as described in https://vscode-docs.readthedocs.io/en/stable/extensions/install-extension/ would be:
VS Code looks for extensions under your extensions folder
.vscode/extensions. Depending on your platform it is located:
Windows %USERPROFILE%\.vscode\extensions
Mac $HOME/.vscode/extensions
Linux $HOME/.vscode/extensions
If you want to load your extension or customization each time VS Code
runs, copy your project to a new folder under .vscode/extensions.
You could write a script to delete / copy over all files.

Having trouble running Fortran on Visual Studio Code

Trying to run Fortran code on VSCode. It is my first time using VSCode, so I am not too familiar.
How do I compile the file? (From what I saw online, it uses the terminal with: "gfortran file.f95". However, this does not work for me.
Is there a way to have a button to build and compile within VSCode?
Knowing that this is an old language the tutorials online are quite outdated.
Thanks in advance!
Yes, it is possible to use VS Code in a Fortran environment. I use the Microsoft developed "CMake Tools" extension for building the program. The "easiest" way, if you are a Windows user, is to install WSL (Ubuntu 20.xx) on Windows 10. Then you have all of the Linux tools such as gfortran and gdb available directly from VS Code. Install these tools the "usual" way in Ubuntu:
sudo apt install build-essential
sudo apt install gfortran
etc.
etc.
Install VS Code for Windows.
Install the VS Code "C/C++ Extension Pack" by Microsoft and enable the insiders edition. That lets you create a project in WSL from native Windows VS Code IDE.
Install the "CMake Tools" extension from Microsoft.
Connect VS Code to WSL and create a folder for your project.
Create hello.f (there are lots of examples on the internet). Here is one example:
program hello
! This is a comment line, it is ignored by the compiler
print *, 'Hello, World!'
end program hello
You need to create a CMakeLists.txt file for your project. Put in same folder as hello.f. Here is a simple one for "hello.f":
# CMake project file for hello.f
cmake_minimum_required (VERSION 3.6)
project (hello_world)
enable_language (Fortran)
# make sure that the default is a RELEASE build
if (NOT CMAKE_BUILD_TYPE)
set (CMAKE_BUILD_TYPE RELEASE CACHE STRING
"Choose the type of build, options are: None Debug Release."
FORCE)
endif (NOT CMAKE_BUILD_TYPE)
# default installation
get_filename_component (default_prefix "hello_world" ABSOLUTE)
set (CMAKE_INSTALL_PREFIX ${default_prefix} CACHE STRING
"Choose the installation directory; by default it installs in the /usr directory."
FORCE)
# FFLAGS depend on the compiler
get_filename_component (Fortran_COMPILER_NAME ${CMAKE_Fortran_COMPILER} NAME)
if (Fortran_COMPILER_NAME MATCHES "gfortran.*")
# gfortran
set (CMAKE_Fortran_FLAGS_RELEASE "-funroll-all-loops -fno-f2c -O3 -std=legacy")
set (CMAKE_Fortran_FLAGS_DEBUG "-fno-f2c -O0 -g -std=legacy")
elseif (Fortran_COMPILER_NAME MATCHES "ifort.*")
# ifort (untested)
set (CMAKE_Fortran_FLAGS_RELEASE "-f77rtl -O3")
set (CMAKE_Fortran_FLAGS_DEBUG "-f77rtl -O0 -g")
elseif (Fortran_COMPILER_NAME MATCHES "g77")
# g77
set (CMAKE_Fortran_FLAGS_RELEASE "-funroll-all-loops -fno-f2c -O3 -m32")
set (CMAKE_Fortran_FLAGS_DEBUG "-fno-f2c -O0 -g -m32")
else (Fortran_COMPILER_NAME MATCHES "gfortran.*")
message ("CMAKE_Fortran_COMPILER full path: " ${CMAKE_Fortran_COMPILER})
message ("Fortran compiler: " ${Fortran_COMPILER_NAME})
message ("No optimized Fortran compiler flags are known, we just try -O2...")
set (CMAKE_Fortran_FLAGS_RELEASE "-O2")
set (CMAKE_Fortran_FLAGS_DEBUG "-O0 -g")
endif (Fortran_COMPILER_NAME MATCHES "gfortran.*")
# build executables
set (EXECUTABLES "hello_world")
add_executable ("hello_world" "hello.f")
# install executables
install (TARGETS ${EXECUTABLES}
RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX})
You also need to create a file called .vscode/launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) hello",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/hello_world",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
}
]
}
Now you are ready to configure, build and debug your program.
Install a Fortran compiler on your system.
For Windows download and install from here : G95 - Windows Download (use recommended option to automatically set the path in windows)
For linux use the package manager to install gfortran package
Linux Example for debian based systems like ubuntu:
sudo apt-get update
sudo apt-get install gfortran
Then after installation you can compile your fortran files you edit in VSCode.
In the Terminal you need to use
g95 –c hello.f90
Compiles hello.f90 to an object file named hello.o
g95 hello.f90
Compiles hello.f90 and links it to produce an executable a.out`
For Linux
gfortran hello.f90