It's just a bullet point.
The most important thing is whether you can from ... import ...
.
The directory of the self-made module and
When both test module directory hierarchies are deep.
It seems that the behavior changes in 3 or more layers.
The sample code when the hierarchy is deep like the following structure, I couldn't find it even if I googled it, so I wrote an article.
.
├tests
│├mod
││└test_module.py
├src
│├mod
││└module.py
I saw many things like the following.
.
├tests
│└test_module.py
├src
│└module.py
What I want to do is How do I run unittest with pytest to separate module code and test code in directories? However, this method requires you to specify module execution options.
In launch.json
, you need to describe the debug configuration of all patterns for the number of modules.
It seems that there is no problem if it is a small project like a tutorial,
The bigger the project, the more nonsense it is.
The contents of the __init__.py
file are all empty.
What's more, you don't need __init__.py
.
Again, it works fine without __init__.py
, and intellisense works.
This is the actual test.
test_name.py
import pytest
from src.animal.mammal import human
from src.star import satellite
def test_human_name():
target=human("Jane Doe")
ans=target.name
assert ans=="Jane Doe"
def test_satellite_name():
target=satellite("lua")
ans=target.name
assert ans=="lua"
def test_human_foot_count():
ans=human.howmanyfoot()
assert ans==2
if __name__ == "__main__":
pass
star.py
class satellite():
def __init__(self, name:str):
self.name=name
def name(self) -> str:
return self.name
class planet():
def __init__(self, name:str):
self.name=name
def name(self) -> str:
return self.name
mammal.py
class human():
def __init__(self, name:str):
self.name=name
@classmethod
def name(self) -> str:
return self.name
@staticmethod
def howmanyfoot() -> int:
return 2
This is an error workaround when processing from import
of" Currrent file debugging ".
By putting this, it seems that it will recognize "Python path" and "self-made module of other hierarchy".
.env
PYTHONPATH=./
It recognizes even if you are using a virtual environment such as conda.
If you execute the following script, You can see if the environment is recognized.
import_test.py
import sys
print(sys.path)
As a result of executing ʻimport_test.pyabove, It is OK if
./src/` is included.
It defines three types of debugging methods. From top to bottom
--Test_name Module debugging --Module debugging of mammal module --Debug the file where the cursor is placed with vscode
In my personal opinion, module debugging is It is limited to applications such as specifying the main module.
In most cases, press the F5
key on the tab being edited to debug the current file → test execution flow.
launch.json
{
//You can use IntelliSense to learn the available attributes.
//Hover and display the description of existing attributes.
//Check the following for more information: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python:Module test_name",
"type": "python",
"request": "launch",
"cwd": "${workspaceFolder}",
"module": "tests.test_mod.test_name"
},{
"name": "Python:Module mammal",
"type": "python",
"request": "launch",
"cwd": "${workspaceFolder}",
"module": "src.animal.mammal"
},
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
]
}
--Folder where the test module is located --Which testing framework to use --Specify a file that describes which python environment to use --Even if you are using a virtual environment such as conda, it will be recognized by this.
settings.json
{
"python.testing.pytestArgs": [
"tests"
],
"python.testing.unittestEnabled": false,
"python.testing.nosetestsEnabled": false,
"python.testing.pytestEnabled": true,
"python.envFile": "${workspaceFolder}/.env"
}
If you look closely, you can see that the location of .env
is specified by default.
Conda
is used up to" Isolation of virtual environment "and" Installation of Python in virtual environment ".
Library installation uses pip
in the virtual environment.Excelsior!
Recommended Posts