https://mooseframework.inl.gov
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
ActionUnitTest Class Referenceabstract

Base class for building basic unit tests for MOOSE actions. More...

#include <ActionUnitTest.h>

Inheritance diagram for ActionUnitTest:
[legend]

Public Member Functions

 ActionUnitTest (const std::string &app_name)
 
void SetUp () override
 

Protected Member Functions

virtual void buildActions ()=0
 Override this to create the action(s) you want to test. More...
 
void buildMinimalObjects ()
 Some base objects we often need to have the action act on them. More...
 
virtual void runActions ()=0
 Override this to make the actions execute NOTE: many tasks will auto-register their task at creation, but for the others, do not forget to register them. More...
 
template<typename T >
T & addObject (const std::string &type, const std::string &name, InputParameters &params)
 Convenience routine for adding an object in an Actions test. More...
 

Protected Attributes

std::unique_ptr< MooseMesh_mesh
 
std::shared_ptr< MooseApp_app
 
Factory_factory
 
ActionFactory_action_factory
 
std::shared_ptr< FEProblem_fe_problem
 

Detailed Description

Base class for building basic unit tests for MOOSE actions.

This class follows the classic actions that are needed in order to build a simulation. To build a unit test, inherit from this class and build your test using the following template:

In your .h file:

class MyUnitTest : public ActionUnitTest { public: MyUnitTest() : ActionUnitTest("MyAppUnitApp") { buildAction(); }

protected:

void buildActions() { // Start with the validParams InputParameters pars = _action_factory.getValidParams("MyActionThatIAmTesting"); // Set the parameters you need pars.set<bool>("do this thing") = true; // Build the action into the action warehouse (just like a meta-action would do)

}

void runActions() { // NOTE: if multiple tasks are required, you have to code them here

const auto task_name = "name of the task we want to test the action on"; // Add the task: only needed if the task does not auto-register // Check that the task is registered mooseAssert(_action_factory.isRegisteredTask(task_name), "Should have registered the task"); // Run the task _app->actionWarehouse().executeActionsWithAction(task_name); }

// member variable used later in the actual tests const MyActionIAmTesting * _action; };

In your .C file

TEST_F(MyActionThatIAmTesting, name of the test) { // Testing individual methods EXPECT_EQ("testing", _action->method(par1, par2)); // Testing if an object has been created by action (if stored in the problem) EXPECT_EQ(true, _fe_problem->hasObjectType(name, 0)); }

NOTE: Testing complex actions that build on other actions may require a deep knowledge of the setup phase in MOOSE. Use Debug/show_actions on a regular simulation to get more information on the setup

Definition at line 78 of file ActionUnitTest.h.

Constructor & Destructor Documentation

◆ ActionUnitTest()

ActionUnitTest::ActionUnitTest ( const std::string &  app_name)
inline
Parameters
app_nameThe name of client's application

Definition at line 84 of file ActionUnitTest.h.

85  : _app(Moose::createMooseApp(app_name, 0, nullptr)),
86  _factory(_app->getFactory()),
87  _action_factory(_app->getActionFactory())
88  {
90  }
std::shared_ptr< MooseApp > _app
void buildMinimalObjects()
Some base objects we often need to have the action act on them.
Factory & _factory
std::unique_ptr< MooseApp > createMooseApp(const std::string &default_app_type, int argc, char *argv[])
Create a MooseApp from command-line arguments.
Definition: MooseMain.C:27
ActionFactory & _action_factory

Member Function Documentation

◆ addObject()

template<typename T >
T & ActionUnitTest::addObject ( const std::string &  type,
const std::string &  name,
InputParameters params 
)
protected

Convenience routine for adding an object in an Actions test.

Definition at line 144 of file ActionUnitTest.h.

147 {
148  auto objects = _fe_problem->addObject<T>(type, name, params);
149  mooseAssert(objects.size() == 1, "Doesn't work with threading");
150  return *objects[0];
151 }
std::string name(const ElemQuality q)
std::shared_ptr< FEProblem > _fe_problem

◆ buildActions()

virtual void ActionUnitTest::buildActions ( )
protectedpure virtual

Override this to create the action(s) you want to test.

Referenced by SetUp().

◆ buildMinimalObjects()

void ActionUnitTest::buildMinimalObjects ( )
inlineprotected

Some base objects we often need to have the action act on them.

Definition at line 103 of file ActionUnitTest.h.

Referenced by ActionUnitTest().

104  {
105  InputParameters mesh_params = _factory.getValidParams("GeneratedMesh");
106  mesh_params.set<MooseEnum>("dim") = "3";
107  mesh_params.set<unsigned int>("nx") = 2;
108  mesh_params.set<unsigned int>("ny") = 2;
109  mesh_params.set<unsigned int>("nz") = 2;
110  _mesh = _factory.createUnique<MooseMesh>("GeneratedMesh", "name1", mesh_params);
111  _mesh->setMeshBase(_mesh->buildMeshBaseObject());
112  _mesh->buildMesh();
113 
114  InputParameters problem_params = _factory.getValidParams("FEProblem");
115  problem_params.set<MooseMesh *>("mesh") = _mesh.get();
116  problem_params.set<std::string>(MooseBase::name_param) = "name2";
117  _fe_problem = _factory.create<FEProblem>("FEProblem", "problem", problem_params);
118 
120 
121  _app->actionWarehouse().problemBase() = _fe_problem;
122  }
static const std::string name_param
The name of the parameter that contains the object name.
Definition: MooseBase.h:55
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
Definition: FEProblem.h:20
std::shared_ptr< MooseApp > _app
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
std::shared_ptr< MooseObject > create(const std::string &obj_name, const std::string &name, const InputParameters &parameters, THREAD_ID tid=0, bool print_deprecated=true)
Definition: Factory.C:142
InputParameters getValidParams(const std::string &name) const
Get valid parameters for the object.
Definition: Factory.C:68
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
Factory & _factory
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
Definition: MooseMesh.h:93
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:54
std::shared_ptr< FEProblem > _fe_problem
std::unique_ptr< MooseObject > createUnique(const std::string &obj_name, const std::string &name, const InputParameters &parameters, THREAD_ID tid=0, bool print_deprecated=true)
Build an object (must be registered) - THIS METHOD IS DEPRECATED (Use create<T>()) ...
Definition: Factory.C:128
std::unique_ptr< MooseMesh > _mesh

◆ runActions()

virtual void ActionUnitTest::runActions ( )
protectedpure virtual

Override this to make the actions execute NOTE: many tasks will auto-register their task at creation, but for the others, do not forget to register them.

To check if a task is registered, use: _action_factory.isRegisteredTask("auto_checkpoint_action")

Referenced by SetUp().

◆ SetUp()

void ActionUnitTest::SetUp ( )
inlineoverride

Definition at line 92 of file ActionUnitTest.h.

93  {
94  buildActions();
95  runActions();
96  }
virtual void runActions()=0
Override this to make the actions execute NOTE: many tasks will auto-register their task at creation...
virtual void buildActions()=0
Override this to create the action(s) you want to test.

Member Data Documentation

◆ _action_factory

ActionFactory& ActionUnitTest::_action_factory
protected

Definition at line 138 of file ActionUnitTest.h.

◆ _app

std::shared_ptr<MooseApp> ActionUnitTest::_app
protected

Definition at line 136 of file ActionUnitTest.h.

Referenced by buildMinimalObjects().

◆ _factory

Factory& ActionUnitTest::_factory
protected

Definition at line 137 of file ActionUnitTest.h.

Referenced by buildMinimalObjects().

◆ _fe_problem

std::shared_ptr<FEProblem> ActionUnitTest::_fe_problem
protected

Definition at line 139 of file ActionUnitTest.h.

Referenced by addObject(), and buildMinimalObjects().

◆ _mesh

std::unique_ptr<MooseMesh> ActionUnitTest::_mesh
protected

Definition at line 135 of file ActionUnitTest.h.

Referenced by buildMinimalObjects().


The documentation for this class was generated from the following file: