【发布时间】:2021-10-16 12:10:21
【问题描述】:
我在不同的目录中有不同的.py文件。
但是,每个类都有大部分相同的函数名称,只是代码方法不同。
我想使用 pytest 一起测试他们的每一个性能。
test_CompositeKey.py:
import os
import pandas as pd
import pytest
from dagster import execute_solid
from CompositeKey.modules.Gordian.Gordian import (add_to_non_key_set, create_prefix_tree, dfs, find_uniqueness_of_keys, get_col_cardinalities, get_final_col_list, get_keyset, non_key_finder, prefix_tree_merging, set_index_to_col_names)
from CompositeKey.modules.Assisted_Gordian.Assisted_Gordian import (add_to_non_key_set, create_prefix_tree, dfs, find_uniqueness_of_keys, get_col_cardinalities, get_final_col_list, get_keyset, non_key_finder, prefix_tree_merging, set_index_to_col_names)
from CompositeKey.modules.Assisted_Gordian_with_Cardinality_Pruning.Assisted_Gordian_with_Cardinality_Pruning import (add_to_non_key_set, create_prefix_tree, dfs, find_uniqueness_of_keys, get_col_cardinalities, get_final_col_list, get_keyset, non_key_finder, prefix_tree_merging, set_index_to_col_names)
from pwmf.pipeline.utils import local_mode
df = pd.read_csv('../data/OPIC-scraped-portfolio-public.csv')
df_sample = df.sample(10)
@pytest.mark.unit
def test_add_to_non_key_set():
curNonKey = {0, 1, 4, 8, 21}
global NonKeySet
NonKeySet = {(0, 1, 4, 21, 6, 8), (0, 1, 4, 21, 6)}
# Changes
observed_value_G = CompositeKey.modules.Gordian.Gordian.add_to_non_key_set(curNonKey, NonKeySet)
observed_value_AG = CompositeKey.modules.Assisted_Gordian.Assisted_Gordian.add_to_non_key_set(curNonKey, NonKeySet)
observed_value_AGC = CompositeKey.modules.Assisted_Gordian_with_Cardinality_Pruning.Assisted_Gordian_with_Cardinality_Pruning.add_to_non_key_set(curNonKey, NonKeySet)
expected_output = {(0, 1, 4, 21, 8), (0, 1, 4, 21, 6, 8), (0, 1, 4, 21, 6)}
assert (expected_output == observed_value_G) and (expected_output == observed_value_AG) and (expected_output == observed_value_AGC)
test_add_to_non_key_set()
错误:
Traceback (most recent call last):
File "test_CompositeKey.py", line 135, in <module>
test_add_to_non_key_set()
File "test_CompositeKey.py", line 21, in test_add_to_non_key_set
observed_value_AG = CompositeKey.modules.Gordian.Assisted_Gordian.add_to_non_key_set(curNonKey, NonKeySet)
NameError: name 'CompositeKey' is not defined
我检查了文件夹路径,它们是正确的。事实上,我有各种其他类,每个类都导入这些行中的一行,没有问题。
在每个导入文件的目录中创建空的 __init__.py 文件没有帮助。
@blhsing 的解决方案:
import os
import pandas as pd
import pytest
from dagster import execute_solid
from CompositeKey.modules.Gordian import Gordian as G
from CompositeKey.modules.Assisted_Gordian import Assisted_Gordian as AG
from CompositeKey.modules.Assisted_Gordian_with_Cardinality_Pruning import Assisted_Gordian_with_Cardinality_Pruning as AGC
from pwmf.pipeline.utils import local_mode
df = pd.read_csv('../data/OPIC-scraped-portfolio-public.csv')
df_sample = df.sample(10)
@pytest.mark.unit
def test_add_to_non_key_set():
curNonKey = {0, 1, 4, 8, 21}
global NonKeySet
NonKeySet = {(0, 1, 4, 21, 6, 8), (0, 1, 4, 21, 6)}
observed_value_G = G.add_to_non_key_set(curNonKey, NonKeySet)
observed_value_AG = AG.add_to_non_key_set(curNonKey, NonKeySet)
observed_value_AGC = AGC.add_to_non_key_set(curNonKey, NonKeySet)
expected_output = {(0, 1, 4, 21, 8), (0, 1, 4, 21, 6, 8), (0, 1, 4, 21, 6)}
assert (expected_output == observed_value_G) and (expected_output == observed_value_AG) and (expected_output == observed_value_AGC)
【问题讨论】:
标签: python-3.x import python-import init