xtd - Reference Guide  0.1.1
Modern c++17/20 framework to create console, GUI and unit test applications on Windows, macOS, Linux, iOS and android.
test_class.h
Go to the documentation of this file.
1 #pragma once
5 #include "../tunit_export.h"
8 #include "settings.h"
10 #include "test_cleanup_attribute.h"
11 #include "test_method_attribute.h"
12 #include <chrono>
13 #include <vector>
14 
16 namespace xtd {
18  namespace tunit {
20  class registered_test_class;
21  class test;
22  class unit_test;
24 
26  public:
28  test_class() = default;
29  test_class(const test_class&) = default;
30  test_class& operator=(const test_class&) = default;
31  virtual ~test_class() = default;
33 
34  std::chrono::milliseconds elapsed_time() const noexcept {
35  using namespace std::chrono_literals;
36  if (start_time_point.time_since_epoch() == 0ms && end_time_point.time_since_epoch() == 0ms) return 0ms;
37  if (end_time_point.time_since_epoch() == 0ms) return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - start_time_point);
38  return std::chrono::duration_cast<std::chrono::milliseconds>(end_time_point - start_time_point);
39  }
40 
41  std::string name() const noexcept {return name_;}
42 
43  size_t test_count() const noexcept {
44  size_t count = 0;
45  for (auto method : tests_)
46  if (settings::default_settings().is_match_test_name(name(), method.name())) count++;
47  return count;
48  }
49 
50  size_t aborted_test_count() const noexcept {
51  size_t count = 0;
52  for (auto method : tests_)
53  if (settings::default_settings().is_match_test_name(name(), method.name()) && method.aborted()) count++;
54  return count;
55  }
56 
57  size_t failed_test_count() const noexcept {
58  size_t count = 0;
59  for (auto method : tests_)
60  if (settings::default_settings().is_match_test_name(name(), method.name()) && method.failed()) count++;
61  return count;
62  }
63 
64  size_t ignored_test_count() const noexcept {
65  size_t count = 0;
66  for (auto method : tests_)
67  if (settings::default_settings().is_match_test_name(name(), method.name()) && method.ignored()) count++;
68  return count;
69  }
70 
71  size_t succeed_test_count() const noexcept {
72  size_t count = 0;
73  for (auto method : tests_)
74  if (settings::default_settings().is_match_test_name(name(), method.name()) && method.succeed()) count++;
75  return count;
76  }
77 
78  const std::vector<xtd::tunit::test>& tests() const noexcept {return tests_;}
79 
80  protected:
81  void add_class_cleanup(const xtd::tunit::test& class_cleanup) noexcept {class_cleanup_ = class_cleanup;}
82 
83  void add_class_initialize(const xtd::tunit::test& class_initialize) noexcept {class_initialize_ = class_initialize;}
84 
85  void add_test_cleanup(const xtd::tunit::test& test_cleanup) noexcept {test_cleanup_ = test_cleanup;}
86 
87  void add_test_initialize(const xtd::tunit::test& test_initialize) noexcept {test_initialize_ = test_initialize;}
88 
89  void add_test_method(const xtd::tunit::test& test) noexcept {tests_.push_back(test);}
90 
91  private:
92  friend class xtd::tunit::test;
93  friend class xtd::tunit::unit_test;
100 
101  const xtd::tunit::test& class_cleanup() const noexcept {return class_cleanup_;}
102  const xtd::tunit::test& class_initialize() const noexcept {return class_initialize_;}
103  const xtd::tunit::test& test_cleanup() const noexcept {return test_cleanup_;}
104  const xtd::tunit::test& test_initialize() const noexcept {return test_initialize_;}
105  const std::vector<xtd::tunit::test>& test_methods() const noexcept {return tests_;}
106 
107  void run(const xtd::tunit::unit_test& unit_test);
108 
111  std::chrono::high_resolution_clock::time_point end_time_point;
112  std::string name_;
113  std::chrono::high_resolution_clock::time_point start_time_point;
114  xtd::tunit::test test_cleanup_;
115  xtd::tunit::test test_initialize_;
116  std::vector<xtd::tunit::test> tests_;
117  };
118  }
119 }
120 
121 #define test_class_(class_name) \
122  class_name;\
123  xtd::tunit::test_class_attribute<class_name> __##class_name##_attribute {#class_name}; \
124  class class_name : public xtd::tunit::test_class
125 
126 #define test_class_from_(class_name, from_class_name) \
127  class_name;\
128  xtd::tunit::test_class_attribute<class_name> __##class_name##_attribute {#class_name}; \
129  class class_name : public from_class_name
130 
Contains xtd::tunit::class_cleanup_attribute class.
Contains xtd::tunit::class_initialize_attribute class.
This attribute is use to add cleanup class method to class test attribute.
Definition: class_cleanup_attribute.h:17
This attribute is use to add initialize class method to class test attribute.
Definition: class_initialize_attribute.h:17
Represents the registered test class.
Definition: registered_test_class.h:19
static xtd::tunit::settings & default_settings() noexcept
Get default settings instance.
Definition: test_class.h:25
Definition: test_cleanup_attribute.h:11
Definition: test_initialize_attribute.h:11
Definition: test_method_attribute.h:12
Definition: test.h:24
The template class.
Definition: unit_test.h:32
#define tunit_export_
Define shared library export.
Definition: tunit_export.h:13
#define class_initialize_(method_name)
add initialize class method to class test.
Definition: class_initialize_attribute.h:42
#define class_cleanup_(method_name)
Add cleanup class method to class test.
Definition: class_cleanup_attribute.h:42
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition: system_report.h:17
Contains xtd::tunit::test_cleanup_attribute class.
Contains xtd::tunit::test_initialize_attribute class.
Contains xtd::tunit::test_method_attribute class.
Contains xtd::tunit::settings class.