Repository: dbt-labs/dbt-core

Issue:
[CT-932] `dbt test` works with click



Discussion:

Resolving patch:
diff --git a/core/dbt/cli/main.py b/core/dbt/cli/main.py
--- a/core/dbt/cli/main.py
+++ b/core/dbt/cli/main.py
@@ -12,6 +12,7 @@
 from dbt.task.clean import CleanTask
 from dbt.task.deps import DepsTask
 from dbt.task.run import RunTask
+from dbt.task.test import TestTask
 
 
 # CLI invocation
@@ -41,10 +42,11 @@ def __init__(
     def invoke(self, args: List[str]) -> Tuple[Optional[List], bool]:
         try:
             dbt_ctx = cli.make_context(cli.name, args)
-            dbt_ctx.obj = {}
-            dbt_ctx.obj["project"] = self.project
-            dbt_ctx.obj["profile"] = self.profile
-            dbt_ctx.obj["manifest"] = self.manifest
+            dbt_ctx.obj = {
+                "project": self.project,
+                "profile": self.profile,
+                "manifest": self.manifest,
+            }
             return cli.invoke(dbt_ctx)
         except (click.NoSuchOption, click.UsageError) as e:
             raise dbtUsageException(e.message)
@@ -206,7 +208,8 @@ def docs_serve(ctx, **kwargs):
 @p.version_check
 @requires.preflight
 def compile(ctx, **kwargs):
-    """Generates executable SQL from source, model, test, and analysis files. Compiled SQL files are written to the target/ directory."""
+    """Generates executable SQL from source, model, test, and analysis files. Compiled SQL files are written to the
+    target/ directory."""
     click.echo(f"`{inspect.stack()[0][3]}` called\n flags: {ctx.obj['flags']}")
     return None, True
 
@@ -449,8 +452,12 @@ def freshness(ctx, **kwargs):
 @requires.preflight
 def test(ctx, **kwargs):
     """Runs tests on data in deployed models. Run this after `dbt run`"""
-    click.echo(f"`{inspect.stack()[0][3]}` called\n flags: {ctx.obj['flags']}")
-    return None, True
+    config = RuntimeConfig.from_parts(ctx.obj["project"], ctx.obj["profile"], ctx.obj["flags"])
+    task = TestTask(ctx.obj["flags"], config)
+
+    results = task.run()
+    success = task.interpret_results(results)
+    return results, success
 
 
 # Support running as a module
diff --git a/core/dbt/config/runtime.py b/core/dbt/config/runtime.py
--- a/core/dbt/config/runtime.py
+++ b/core/dbt/config/runtime.py
@@ -17,13 +17,14 @@
 
 from dbt import flags
 from dbt.adapters.factory import get_include_paths, get_relation_class_by_name
-from dbt.config.profile import read_user_config
 from dbt.config.project import load_raw_project
 from dbt.contracts.connection import AdapterRequiredConfig, Credentials, HasCredentials
 from dbt.contracts.graph.manifest import ManifestMetadata
 from dbt.contracts.project import Configuration, UserConfig
 from dbt.contracts.relation import ComponentName
 from dbt.dataclass_schema import ValidationError
+from dbt.events.functions import warn_or_error
+from dbt.events.types import UnusedResourceConfigPath
 from dbt.exceptions import (
     ConfigContractBroken,
     DbtProjectError,
@@ -31,10 +32,7 @@
     RuntimeException,
     UninstalledPackagesFound,
 )
-from dbt.events.functions import warn_or_error
-from dbt.events.types import UnusedResourceConfigPath
 from dbt.helper_types import DictDefaultEmptyStr, FQNPath, PathSet
-
 from .profile import Profile
 from .project import Project
 from .renderer import DbtProjectYamlRenderer, ProfileRenderer
diff --git a/core/dbt/task/deps.py b/core/dbt/task/deps.py
--- a/core/dbt/task/deps.py
+++ b/core/dbt/task/deps.py
@@ -25,7 +25,6 @@
 
 from dbt.task.base import BaseTask, move_to_nearest_project_dir
 
-
 from dbt.config import Project
 
 
@@ -91,4 +90,4 @@ def run(self) -> None:
                 )
             if packages_to_upgrade:
                 fire_event(EmptyLine())
-                fire_event(DepsNotifyUpdatesAvailable(packages=ListOfStrings(packages_to_upgrade)))
\ No newline at end of file
+                fire_event(DepsNotifyUpdatesAvailable(packages=ListOfStrings(packages_to_upgrade)))


Test patch: