Skip to content

[C#] Add unit tests for C# APIs and models #2007

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public CSharpClientCodegen() {
apiPackage = "IO.Swagger.Api";
modelPackage = "IO.Swagger.Model";

modelTestTemplateFiles.put("model_test.mustache", ".cs");
apiTestTemplateFiles.put("api_test.mustache", ".cs");

reservedWords = new HashSet<String>(
Arrays.asList(
// local variable names in API methods (endpoints)
Expand Down Expand Up @@ -270,6 +273,16 @@ public String escapeReservedWord(String name) {
return "_" + name;
}

@Override
public String apiTestFileFolder() {
return outputFolder + ".Test";
}

@Override
public String modelTestFileFolder() {
return outputFolder + ".Test";
}

@Override
public String apiFileFolder() {
return outputFolder + File.separator + sourceFolder + File.separator + apiPackage().replace('.', File.separatorChar);
Expand Down Expand Up @@ -342,6 +355,16 @@ public String toModelFilename(String name) {
return toModelName(name);
}

@Override
public String toApiTestFilename(String name) {
return toApiName(name) + "Tests";
}

@Override
public String toModelTestFilename(String name) {
return toModelName(name) + "Tests";
}

@Override
public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
super.postProcessOperations(objs);
Expand Down
8 changes: 7 additions & 1 deletion modules/swagger-codegen/src/main/resources/csharp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,11 @@ NOTE: The DLLs included in the package may not be the latest version. We recommn
```
Install-Package RestSharp
Install-Package Newtonsoft.Json
```
```

## Installation
Run the following command to generate the DLL
- [Mac/Linux] compile-mono.sh
- [Windows] compile.bat

Then include the DLL (under the `bin` folder) in the C# project
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using System.IO;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using RestSharp;
using NUnit.Framework;

using {{packageName}}.Client;
using {{packageName}}.Api;
{{#hasImport}}using {{packageName}}.Model;
{{/hasImport}}

namespace {{packageName}}.Test
{
[TestFixture]
public class {{classname}}Tests
{
private {{classname}} instance;

/// <summary>
/// Setup before each unit test
/// </summary>
[SetUp]
public void Init()
{
instance = new {{classname}}();
}

/// <summary>
/// Clean up after each unit test
/// </summary>
[TearDown]
public void Cleanup()
{

}

/// <summary>
/// Test an instance of {{classname}}
/// </summary>
[Test]
public void {{operationId}}InstanceTest()
{
Assert.IsInstanceOf<{{classname}}> (instance, "instance is a {{classname}}");
}

{{#operations}}{{#operation}}
/// <summary>
/// Test {{operationId}}
/// </summary>
[Test]
public void {{operationId}}Test()
{
// TODO: add unit test for the method '{{operationId}}'
{{#allParams}}{{{dataType}}} {{paramName}} = null; // TODO: replace null with proper value
{{/allParams}}
{{#returnType}}var response = {{/returnType}}instance.{{operationId}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
{{#returnType}}Assert.IsInstanceOf<{{{returnType}}}> (response, "response is {{{returnType}}}");{{/returnType}}
}
{{/operation}}{{/operations}}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using NUnit.Framework;

using System;
using System.Linq;
using System.IO;
using System.Collections.Generic;
using {{packageName}}.Api;
using {{packageName}}.Model;
using {{packageName}}.Client;
using System.Reflection;

{{#models}}
{{#model}}
namespace {{packageName}}.Test
{
[TestFixture]
public class {{classname}}Tests
{
private {{classname}} instance;

/// <summary>
/// Setup before each test
/// </summary>
[SetUp]
public void Init()
{
instance = new {{classname}}();
}

/// <summary>
/// Clean up after each test
/// </summary>
[TearDown]
public void Cleanup()
{

}

/// <summary>
/// Test an instance of {{classname}}
/// </summary>
[Test]
public void {{classname}}InstanceTest()
{
Assert.IsInstanceOf<{{classname}}> (instance, "instance is a {{classname}}");
}

{{#vars}}
/// <summary>
/// Test the property '{{name}}'
/// </summary>
[Test]
public void {{name}}Test()
{
// TODO: unit test for the property '{{name}}'
}
{{/vars}}

}

}
{{/model}}
{{/models}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using NUnit.Framework;

using System;
using System.Linq;
using System.IO;
using System.Collections.Generic;
using IO.Swagger.Api;
using IO.Swagger.Model;
using IO.Swagger.Client;
using System.Reflection;

namespace IO.Swagger.Test
{
[TestFixture]
public class CategoryTests
{
private Category instance;

/// <summary>
/// Setup before each test
/// </summary>
[SetUp]
public void Init()
{
instance = new Category();
}

/// <summary>
/// Clean up after each test
/// </summary>
[TearDown]
public void Cleanup()
{

}

/// <summary>
/// Test an instance of Category
/// </summary>
[Test]
public void CategoryInstanceTest()
{
Assert.IsInstanceOf<Category> (instance, "instance is a Category");
}


/// <summary>
/// Test the property 'Id'
/// </summary>
[Test]
public void IdTest()
{
// TODO: unit test for the property 'Id'
}

/// <summary>
/// Test the property 'Name'
/// </summary>
[Test]
public void NameTest()
{
// TODO: unit test for the property 'Name'
}


}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using NUnit.Framework;

using System;
using System.Linq;
using System.IO;
using System.Collections.Generic;
using IO.Swagger.Api;
using IO.Swagger.Model;
using IO.Swagger.Client;
using System.Reflection;

namespace IO.Swagger.Test
{
[TestFixture]
public class OrderTests
{
private Order instance;

/// <summary>
/// Setup before each test
/// </summary>
[SetUp]
public void Init()
{
instance = new Order();
}

/// <summary>
/// Clean up after each test
/// </summary>
[TearDown]
public void Cleanup()
{

}

/// <summary>
/// Test an instance of Order
/// </summary>
[Test]
public void OrderInstanceTest()
{
Assert.IsInstanceOf<Order> (instance, "instance is a Order");
}


/// <summary>
/// Test the property 'Id'
/// </summary>
[Test]
public void IdTest()
{
// TODO: unit test for the property 'Id'
}

/// <summary>
/// Test the property 'PetId'
/// </summary>
[Test]
public void PetIdTest()
{
// TODO: unit test for the property 'PetId'
}

/// <summary>
/// Test the property 'Quantity'
/// </summary>
[Test]
public void QuantityTest()
{
// TODO: unit test for the property 'Quantity'
}

/// <summary>
/// Test the property 'ShipDate'
/// </summary>
[Test]
public void ShipDateTest()
{
// TODO: unit test for the property 'ShipDate'
}

/// <summary>
/// Test the property 'Status'
/// </summary>
[Test]
public void StatusTest()
{
// TODO: unit test for the property 'Status'
}

/// <summary>
/// Test the property 'Complete'
/// </summary>
[Test]
public void CompleteTest()
{
// TODO: unit test for the property 'Complete'
}


}

}
Loading