首页 > 其他分享 >dotnet hello world

dotnet hello world

时间:2024-08-03 22:51:31浏览次数:6  
标签:Tests csproj 单元测试 HelloDotnet new dotnet world hello

参考资料

dotnet 命令参考

使用 dotnet test 和 xUnit 在 .NET 中对 C# 进行单元测试

Declaring InternalsVisibleTo in the csproj

XUnit输出消息

创建控制台项目

# 创建项目目录
md DotnetStudy
cd DotnetStudy

# 创建解决方案
dotnet new sln
# 创建控制台项目,-n: 名称,--use-program-main: 不适用顶级语句
dotnet new console -n HelloDotnet --use-program-main
# 将项目添加到解决方案
dotnet sln add HelloDotnet\HelloDotnet.csproj
# 编译
dotnet build --configuration Release
# 运行
dotnet run --configuration Release --project HelloDotnet\HelloDotnet.csproj

添加一个类

namespace HelloDotnet
{
    internal class Calc
    {
        public int Add(int a, int b)
        {
            return a + b;
        }
    }
}

添加单元测试项目

# 创建 xunit 项目
dotnet new xunit -o HelloDotnet.Tests
# 将 xunit 项目添加到解决方案
dotnet sln add HelloDotnet.Tests\HelloDotnet.Tests.csproj
# 将控制台项目作为依赖添加到 xunit 项目
dotnet add HelloDotnet.Tests\HelloDotnet.Tests.csproj reference HelloDotnet\HelloDotnet.csproj

设置单元测试可访问 internal

<Project Sdk="Microsoft.NET.Sdk">

	<PropertyGroup>
		<OutputType>Exe</OutputType>
		<TargetFramework>net8.0</TargetFramework>
		<ImplicitUsings>enable</ImplicitUsings>
		<Nullable>enable</Nullable>
	</PropertyGroup>

	<!-- 设置单元测试可访问 internal -->
	<ItemGroup>
		<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
			<_Parameter1>HelloDotnet.Tests</_Parameter1>
		</AssemblyAttribute>
	</ItemGroup>
</Project>

单元测试代码

namespace HelloDotnet.Tests;

public class UnitTest1
{
    [Fact]
    public void Test1()
    {
        Calc calc = new();
        Assert.Equal(3, calc.Add(1, 2));
    }
}

运行单元测试

dotnet test HelloDotnet.Tests\HelloDotnet.Tests.csproj

关于单元测试的输出

using Xunit.Abstractions;

namespace HelloDotnet.Tests;

public class UnitTest1
{

    ITestOutputHelper output;

    public UnitTest1(ITestOutputHelper output)
    {
        this.output = output;
    }

    [Fact]
    public void Test1()
    {
        output.WriteLine("add test");
        Calc calc = new();
        Assert.Equal(3, calc.Add(1, 2));
    }
}

image

gitignore

dotnet new gitignore

标签:Tests,csproj,单元测试,HelloDotnet,new,dotnet,world,hello
From: https://www.cnblogs.com/bibleghost/p/18341255

相关文章

  • 开发调试驱动helloworld
    开发调试驱动helloworldhttps://learn.microsoft.com/zh-cn/windows-hardware/drivers配置开发环境https://learn.microsoft.com/zh-cn/windows-hardware/drivers/download-the-wdk按照步骤依次安装VisualStudioCommunity、SDK、WDK这里的windbg界面更现代一点https://lea......
  • webservice hello
    一、//hello.hintns__hello(std::stringname,std::string&greeting);二、//helloclient.cpp#include"soapH.h"#include"ns.nsmap"#include<string>#include<iostream>usingnamespacestd;inthello(structsoap*so......
  • 信步漫谈之Android——HelloWorld
    目录目标1资源2第一个HelloWorld程序3项目结构说明3.1目录结构3.2结构说明4在App中添加日志后续补充参考资料目标学习搭建Android的开发环境sayhelloworld1资源官网教程:https://developer.android.com/courses开发工具AndroidStudio下载路径:https://d......
  • 信步漫谈之微信小程序——HelloWorld
    目录目标1资源2程序目录说明3第一个HelloWorld程序4真机调试参考资料(感谢)目标微信小程序开发环境sayhelloworld1资源微信官方文档:https://developers.weixin.qq.com/doc/微信开发者工具下载:https://developers.weixin.qq.com/miniprogram/dev/devtools/downloa......
  • Electron学习笔记(二)Hello World
    目录前言运行主进程创建界面使用窗口打开界面管理窗口的生命周期关闭所有窗口时退出应用(Windows&Linux)​如果没有窗口打开则打开一个窗口(macOS)使用预加载脚本访问渲染器的Node.js添加你自己的功能完整代码展示效果展示前言接上一篇文章Electron学习笔......
  • 论文阅读:Borrowing wisdom from world: modeling rich external knowledge for Chines
    问题定义由于词级中文NER存在第三方解析器分割的边界错误,因此考虑将字符级NER作为默认设置。使用'BMES'标记方案进行字符级NER,将标记表述为序列标记问题。即,对于句子\(s={c_1,...,c_n}\)中的每个字符\(c_i\),使用标签集中的标签进行标记\(L={B,M,E,S,O}\)。O:非实体元素B:实......
  • QOJ7899 Say Hello to the Future
    考虑先求出原序列的方案数设\(f_i\)表示\(1\simi\)被划分为若干区间的方案数,若一段区间合法当且仅当\(r-l+1\ge\max\{a_{l\simr}\}\),可以发现数据结构难以维护且由于不是最优性问题,考虑\(\texttt{cdq}\)分治优化对于每个分治中心\(m\),令\(mxL_i=\max\{a_{i\si......
  • 【Blog1】PyCharm写hello world
    创建项目选择项目类型和项目目录设置python编译器选择编译器新建Python文件编写代码并运行运行运行结果......
  • 最长的Hello, World!(C++)
    最长的Hello,World!(C++)#include<iostream>#include<string>#include<vector>#include<memory>#include<random>_<typenameT>classNode{public:Tdata;std::shared_ptr<Node<T>>next;Node(......
  • 最长的Hello, World!(Python)
    最长的Hello,World!(Python)(lambda_,__,___,____,_____,______,_______,________:getattr(__import__(True.__class__.__name__[_]+[].__class__.__name__[__]),().__class__.__eq__.__class__.__name__[:__]+().__iter__().__cla......