了解测试套件

时间:2018-06-12 16:16:40

标签: scala testing scalatest

我正在学习排名并对套房有疑问。我想测试一下

from datetime import datetime, timedelta
from sqlalchemy import and_
import json

from airflow import DAG
from airflow.models import TaskInstance, DagRun
from airflow.utils.db import provide_session

from airflow.operators.python_operator import PythonOperator


default_args = {'start_date': datetime(2018, 6, 11),
                'retries': 2,
                'retry_delay': timedelta(minutes=2),
                'email': [],
                'email_on_failure': True}


dag = DAG('__RESET__FAILED_TASKS',
          default_args=default_args,
          schedule_interval='@daily',
          catchup=False
          )


@provide_session
def check_py(session=None, **kwargs):

    relevant_task_id = 'relevant_task_id'

    obj = (session
           .query(TaskInstance)
           .filter(and_(TaskInstance.task_id == relevant_task_id,
                        TaskInstance.state == 'failed'))
           .all())

    if obj is None:
        raise KeyError('No failed Task Instances of {} exist.'.format(relevant_task_id))
    else:
        # Clear the relevant tasks.
        (session
         .query(TaskInstance)
         .filter(and_(TaskInstance.task_id == relevant_task_id,
                      TaskInstance.state == 'failed'))
         .delete())

        # Clear downstream tasks and set relevant DAG state to RUNNING
        for _ in obj:
            _ = json.loads(_.val)

            # OPTIONAL: Clear downstream tasks in the specified Dag Run.
            for task in _['downstream_tasks']:
                (session
                 .query(TaskInstance)
                 .filter(and_(TaskInstance.task_id == task,
                              TaskInstance.dag_id == _['dag_id'],
                              TaskInstance.execution_date == datetime.strptime(_['ts'],
                                                                                "%Y-%m-%dT%H:%M:%S")))
                 .delete())

            # Set the Dag Run state to "running"
            dag_run = (session
                       .query(DagRun)
                       .filter(and_(DagRun.dag_id == _['dag_id'],
                                    DagRun.execution_date == datetime.strptime(_['ts'],
                                                                               "%Y-%m-%dT%H:%M:%S")))
                       .first())

            dag_run.set_state('running')

with dag:

    run_check = PythonOperator(task_id='run_check',
                               python_callable=check_py,
                               provide_context=True)

    run_check

从逻辑上讲,我将测试用例分为以下两种:

  1. 空字节来源
  2. 非空字节源
  3. 问题是将案例拆分成不同的套房是否正确:

    select * from mara inner join mvke on mvke~matnr = mara~matnr into table @data(lt_combined).    
    loop at lt_combined into data(ls_combined).
      write: / ls_combined-mara-matnr, ls_combined-mvke-vkorg.
    endloop.
    

    或者select mara~matnr, mvke~vkorg from mara inner join mvke on mvke~matnr = mara~matnr into table @data(lt_combined). loop at lt_combined into data(ls_combined). write: / ls_combined-matnr, ls_combined-vkorg. endloop. 不太适合这种情况?

1 个答案:

答案 0 :(得分:2)

FunSpec旨在提供最小的结构,因此这里没有硬规则。意见结构的一个例子是WordSpec。我要提出的一个建议是通过外部describe("A ByteSource")

清楚地确定测试的主题
class ByteSourceTest extends FunSpec with Matchers {
  describe("A ByteSource") {
    describe("when empty") {
      val byteSource = new ByteSource(new Array[Byte](0))

      it("should have size 0") {
        assert(byteSource.src.size == 0)
      }

      it("should produce NoSuchElementException when head is invoked") {
        assertThrows[NoSuchElementException] {
          byteSource.src.head
        }
      }
    }

    describe("when non-empty") {
      val byteSource = new ByteSource(new Array[Byte](10))

      it("should have size > 0") {
        assert(byteSource.src.size > 0)
      }

      it("should not produce NoSuchElementException when head is invoked") {
        noException shouldBe thrownBy {
          byteSource.src.head
        }
      }
    }
  }
}

测试主题的输出看起来像是自然语言的规范:

A ByteSource
  when empty
  - should have size 0
  - should produce NoSuchElementException when head is invoked
  when non-empty
  - should have size > 0
  - should not produce NoSuchElementException when head is invoked